Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Save Dialog using JFIleChooser.  RSS feed

 
Drake Silver
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • 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:
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to swing...
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!