How to save file when Open button/ or Double click pressed in "showOpenDialog"
Joined: Jul 29, 2012
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".
Joined: Jun 09, 2003
> "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
Joined: Oct 13, 2005
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.
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?
Joined: Jul 29, 2012
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 .