wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Unable to save a file using JFileChooser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Unable to save a file using JFileChooser" Watch "Unable to save a file using JFileChooser" New topic
Author

Unable to save a file using JFileChooser

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Hi,

Please have a look at the following code



In this code, in the class "SaveOperation" I am trying to save a text file in a user defined location. But I am not getting anything as an output, not even an error! Apart from that, I am trying to save this in using a already given name by me, but I doubt whether it will work because the save window has a place to enter the file name. Please help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, if it were me then instead of this code:


I would do this instead:


However that isn't relevant to your problem. Unfortunately your description of the problem doesn't say "I saw the dialog box from line 216" or "I saw the dialog box from line 222" or "I didn't see either of those dialog boxes". Whichever of those is true tells you which code was executed. This is the information you need.

Edit: Also I think you should fix your code so the user can only choose a directory in that class.

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Hi, Thanks a lot for the reply. I see that save dialogbox when I click the button. But when I click the "save" button in the save box, nothing happens! It is not getting closed, no error, nothing get saved! Please help..

And thanks for the correction of the IO part. I will edit it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay. So line 216 isn't run and neither is line 222. Look at your code, then. What would cause that? Well, they are both controlled by an if-statement which I expect you are making a false assumption about.
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Thanks for the reply..

I will do a deeper check on it. Anyway Do you suspect this line may be wrong?

folder = fileChooser.getSelectedFile();
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No. It looks to me like

causes the following code to not be executed because "folder.isDirectory()" returns false.

But why are you speculating about such things here? Just debug your code and find out for certain what is happening.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i'm not going to read 266 lines of code, but even though this code i am about to post saves an image instead of text, the question is about JFileChooser so it might help.

i don't see how you can control the file name using JFileChooser. like you said, the user gets to choose the name. i believe you can limit the folder though. read the JFileChooser API.


SCJP
Visit my download page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  18

You can definitely get rid of the if (fileName == null) return; line. File.getPath() will never ever return null, so fileName will never ever be null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Yohan Weerasinghe wrote:

JFileChooser's save dialog is modal, so the fileSelectionMode will be set only after the dialog is shown and dismissed. I'm guessing that isn't what you wanted, but it's difficult to tell with so much code that's irrelevant to the stated problem. Learn to write an effective SSCCE and you'll get better help sooner.

Also note that your title is off the mark: JFileChooser, as its name implies, is for choosing (or selecting) a file. Not for saving one.


Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Hi all,

Thanks a lot for the help and pardon me for the late reply. As you all suggested, the error was inside the MAIN IF condition. I had to call that "ApproveOption()" method manually, and it worked.. Thanks a lot for all the help.. I really appreciate it..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to save a file using JFileChooser