This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to save file when Open button/ or Double click pressed in Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to save file when Open button/ or Double click pressed in "showOpenDialog"" Watch "How to save file when Open button/ or Double click pressed in "showOpenDialog"" New topic
Author

How to save file when Open button/ or Double click pressed in "showOpenDialog"

Sunny Virtualian
Greenhorn

Joined: Jul 29, 2012
Posts: 19
Hi freinds,
I am facing a problem hope your suggestion will help me in solving this problem.

I have written a class Chooser, Only problem is this,"I wanna save the file when Open button/Double click is pressed in showOpenDialog button".


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> "I wanna save the file when Open button/Double click is pressed in showOpenDialog button".

that makes no sense - why not use the saveDialog (which will have a 'save' button)

if you want nothing to happen when the button is single-clicked, but saved when double-clicked,
iterate the fileChooser's components to get the save button then
1) remove the actionListener/s from that button (or override the chooser's approveSelection())
2) add a mouseListener to the button and add the double-click code
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38031
    
  22
No, that won’t work. And I could tell that without reading any of the code, except that I saw a \n character, which you shouldn’t use. Look here to find out what to use instead.

You are going about the problem quite wrongly. What you should do is to create a little application which uses the file chooser, and get that working. You can find more examples of how file choosers work here. Go through that and learn how to use a chooser to open a file. I cannot remember whether you can use a chooser to create a file, maybe you can’t. But you can find that by reading the link I showed you and the documentation. Note that many methods in the file chooser class require an argument for the Component the dialogue appears over. If you don’t have such a component, you can pass null and the dialogue will appear in the centre of the screen. As I said, create a method which finds a File and returns it.
Then use a System.out to verify you have got the file: System.out.println(f);
Do not try finding a file and writing it in the same method. Create a method which finds the file, and another method which writes to it. You are wrong to put that sort of code into the class’ constructor.
When you have those methods working, you can create a utility class, and put those methods into that class for use in future. search my posts in this forum and “Java in General” for “utility class”, and you should find some hints.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

so are you saying you want to save the existing file, before opening a new file? or are you trying to completely ignore the point of open/save dialog boxes?
Sunny Virtualian
Greenhorn

Joined: Jul 29, 2012
Posts: 19
thanks to all for participating in discussion. The problem has been solved by introducing a File variable, and saving file in that variable from getSelectedFile();
Special thanks to sir Campbell Ritchie .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to save file when Open button/ or Double click pressed in "showOpenDialog"
 
Similar Threads
Really opening and saving REAL files!!!???
JfileChooser selecting existing file.......?
JFileChooser : User selected directory & programmatically created file name : How?
TextFileIn
adding a save method