• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".


 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> "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
 
Marshal
Posts: 76399
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Paper beats rock. Scissors beats tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic