• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to save a file using JFileChooser

 
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
reply
    Bookmark Topic Watch Topic
  • New Topic