PDA

View Full Version : Question about JFileChooser




jtalerico
Feb 15, 2006, 08:01 PM
So I am trying to open a file using JFileChooser... What does the Open button return? Do i have to set a actionListener for it? Because there is a Open button there by default in OS X. Any idea how to set an actionListener to something that is already created?



bousozoku
Feb 15, 2006, 08:11 PM
I'm not sure what the return code since I haven't looked it up recently. Here is a sample of my code where I'm trying to get the path to a file.


public void protein1MouseClicked(java.awt.event.MouseEvent e)
{

JFileChooser fc = new JFileChooser();

int returnCode = fc.showDialog(FPFrame.this, "Select");
if(returnCode == JFileChooser.APPROVE_OPTION)
{
file1 = fc.getSelectedFile();
}
} // end protein1MouseClicked()

jtalerico
Feb 15, 2006, 08:29 PM
I'm not sure what the return code since I haven't looked it up recently. Here is a sample of my code where I'm trying to get the path to a file.


public void protein1MouseClicked(java.awt.event.MouseEvent e)
{

JFileChooser fc = new JFileChooser();

int returnCode = fc.showDialog(FPFrame.this, "Select");
if(returnCode == JFileChooser.APPROVE_OPTION)
{
file1 = fc.getSelectedFile();
}
} // end protein1MouseClicked()


I am with you until FPFrame.this, what exactly is that? What componet?

bousozoku
Feb 15, 2006, 09:16 PM
I am with you until FPFrame.this, what exactly is that? What componet?

It's the parent window/container, a JFrame.

tuc
Feb 15, 2006, 11:38 PM
btw, you might want to use a java.awt.FileChooser
instead of JFileChooser. You get the OS native widget
instead of an approximation.

MarkCollette
Feb 16, 2006, 06:13 PM
If your app is in Swing, then keep it all swing, and stick with JFileChooser.

The JFrame argument to the JFileChooser allows it to attach the JFileChooser dialog onto that JFrame, so it can modally block input to the JFrame.

bousozoku
Feb 16, 2006, 07:43 PM
btw, you might want to use a java.awt.FileChooser
instead of JFileChooser. You get the OS native widget
instead of an approximation.

The extra behaviours that come with JFileChooser far outweigh its not being native. Besides, AWT-based applications probably still don't look the same deployed on a number of platforms. :(

jtalerico
Feb 16, 2006, 07:45 PM
No worries, i have written my own little Client appy... It is hidious.. I have never been to good at the GUI stuff.. Anyone suggest some good GUI hints?

HiRez
Feb 16, 2006, 08:12 PM
If you're doing anything with Swing, I highly recommend the book Core Java Foundation Classes (http://www.amazon.com/gp/product/0130803014/qid=1140142148/sr=1-1/ref=sr_1_1/104-7633462-8487956?s=books&v=glance&n=283155) by Kim Topley. This book is fantastic and saved me a lot of pain back when I was learning Swing programming. IF it's still in print/available, not sure about that. I'm not doing Java development today, but if I were, I know I'd still be referencing this book.

jtalerico
Feb 16, 2006, 08:19 PM
Yeah, I am not in school right now. I am doing a coop here in Raleigh. I am trying to teach myself some stuff, and build myself a program that I can use :)