• 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

Save Dialog using JFIleChooser.

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me why this will not save.
I got the code from another post, seeing as how there isn't a savedialog in Jbuilder component.
anyway, my thought on this is that it's not actually reading the data and saving it.
But with java I'm not sure...don't know what is done automatically and what's not.
here's the code:
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to swing...
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JFileChooser doesn't save your data automatically... it just provides the user a nice way to provide the file where they want to save the data...

After the :


lines you should make sure that the result value is JFileChooser.APPROVE_OPTION, otherwise the user cancelled out of the save. Then open an OutputStream or a Writer on the file returned from JFileChooser.getSelectedFile(). You'll probably want to look up some resources on java.io to see how you want to write your data to the file.
 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
alright, thank you athan.
I'll see what I can find.
 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, why would i call System.exit() in a JFileChooser Dialog?
would that close the whole program, or jut the dialog window?
I was thinking of using something like:

Also, I was wondering.
When do I close a stream...In my Book, it talks about opening buffers and streams and closing the stream..in.close, do i still have to close that stream if I'm using JFileChooser.
If so, how do I close an output stream, for saving files.
out.close, doesn't work?
[ November 22, 2002: Message edited by: Drake Silver ]
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JFileChooser has nothing to do with streams or buffers. As Nathan told you, it provides only a simple way to get the location, where to save a file. It is up to you, to do the whole saving stuff. Btw. System.exit(0) closes the whole application, not only the JFileChooser.
If you open a stream (FileOutputStream), then you have to flush the data before calling the close method, just to be shure, to save the file.
 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok...?
so ..how's this then.
my problem is that I don't think it's taking the text from the text area and then saving it...
I get a null pointer exception when trying to save.
EDITED:
No, i think i know what the problem is...
In saveFile.getSelectedFile(), i'm trying to pick a file...but from where?
So..I am point to the wrong spot...I should be doing something like
saveFile.GetSelectedFile(TextArea.getText())
right?
[ November 22, 2002: Message edited by: Drake Silver ]
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only error, which i can see is in the line
saveFile.setCurrentDirectory(new File("*.srt"));
This is no directory name. You should specify either "." or "C:/tmp/andsoon" - even a directory.
I think, this will be the reason for your NullPointerException.
reply
    Bookmark Topic Watch Topic
  • New Topic