File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes closing a jFileChooser window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "closing a jFileChooser window" Watch "closing a jFileChooser window" New topic
Author

closing a jFileChooser window

Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Hey Java Gurus!,

I have a very simple question. I have a jFileChooser window open and need to have it close when the user selects "cancel". I would like it to only close the jFileChooser window. In my 5 minute career using Swing I've used methods like jframe.dispose() to do this. It doesn't work here. Any suggestions we be appreciated greatly.


BONUS QUESTION: My "open" button works, but does not close after the task is complete. Should I add "finally" to the APPROVE_SELECTION logic and close the window the same way?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
I have a jFileChooser window open and need to have it close when the user selects "cancel".


I'm not sure what you are doing, this is the default behaviour. I suggest you read the JFileChooser API and follow the link to the Swing tutorial on 'How to Use File Choosers" for working examples.
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
If this is the default behavior, why isn't it working??? I would have to override it somewhere, correct???
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
Adam Confino wrote:


You are doing this the hard way.

Instead of adding the JFileChooser as the content pane of your Frame, simply call fileChooser.chooser.showOpenDialog(parent) and you won't have to mess with ActionListeners or closing anything. [Here parent may be null, though preferably not.]

The Mr. Camick's link has a basic code example.

[And I guess my link does too. When did this auto-linking to javadocs happen?]

(Fixed the quoting -- PC -- and yeah, the javadoc auto-linking is great, isn't it?)
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Brian and Rob,

I appreciate yours posts and read the suggested reading, but I still don't quite understand. How would adding the JFileChoose to a contentPane undo the default behavior of the JFileChoose buttons (which it does in this case)?

If I create a JFileChoose as Brian suggested (code below), how is it useful without using an ActionListener? I assume it returns the name of the file as a String??? I've only seen examples using ActionListeners.

I do apologize if my questions are basic. I think there is a lot going on under the covers I don't understand.


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

From your questions it's apparent you haven't read the tutorial. Go to the JFileChooser documentation (I just made a link to it there) and follow the link to the tutorial for decent examples.
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
...
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
Adam Confino wrote:
I appreciate yours posts and read the suggested reading, but I still don't quite understand. How would adding the JFileChoose to a contentPane undo the default behavior of the JFileChoose buttons (which it does in this case)?


The default behavior of the buttons is to close the dialog that pops up when showOpenDialog()/showSaveDialog()/showDialog() are called. If you instead add the JFileChooser to your own frame, all bets are off.


If I create a JFileChoose as Brian suggested (code below), how is it useful without using an ActionListener? I assume it returns the name of the file as a String???


You call fileChooser.chooser.getSelectedFile(), after first checking to see if the user canceled instead of choosing a file. Since you say you read the suggested reading, you must have seen this. I have my doubts, though, so here is the basic code example mentioned above, copied from the javadoc page:



Btw, Paul Clapham took time out of his busy day just to try to help you with this silly little problem. In general I would recommend that you listen to what he has to say, as opposed to snipping at him. This goes even had he accused you of something unwarranted, which (IMHO) he did not. I'm not the boss of you, though, so do as you will.
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
To Brian and Paul,

Again, I thank you for your time. I have, and will state again, that I am appreciative of any of your help. I know some people try to take advantage of that. In this case, I just genuinely don't understand what I've read on the tutorial. That's my problem, not anyone elses.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Okay, no problem. So ask questions about the example code given in the tutorial. There's only half a dozen lines of code required to choose a file using JFileChooser's showOpenDialog method so it shouldn't be hard to clear them up.
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Paul,

Like I said, I appreciate your help. I will also take your advice and refer questions directly to the tutorial examples. I think Brian's last post helped me put it all together.
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
IT WORKED! Thanks for the example. It was the JFrame.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
IT WORKED! Thanks for the example.


And why was it so hard to read the tutorial? The example was given in the tutorial!

We should not have to sit here and ask you 4-5 times to read the tutorial and download the sample code.

We sure are entitled to question whether you read the tutorial, or not, because the code you posted looked nothing like the code from the tutorial.

If you want help in the future, don't tell us how to answer questions, because we know what we are talking about. Instead take a look in the mirror and actually listen to what we are suggesting.

Frankly, you wasted the time of three people trying to help you. I suspect you won't be as lucky on your next question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: closing a jFileChooser window