• 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

JfileChooser save without a dialog box popping up

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was wondering if anyone can help. I have an application that has a text field I want to use Jfilechooser to save or save As the data.

If the file already exisits i want the end user to be able to hit save without the dialog box popping up asking for a file name folder etc, like it would in MS Word etc..

Is this possible?

I'd really appreciate any help

Thanks

Nuala
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It isn't clear to me what you want. You've told us some things you don't want, but not what you do want.

So: Do you want a JFileChooser, but built into your GUI instead of in a popup dialog?

Or do you just want the user to key the full path to the output file into a text field instead of letting them find it via a JFileChooser?

You said "If the file already exists" but I couldn't tell what file that was or how the code would know about it.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait a minute... you mentioned the way MS Word works, in other words you want to save the data in the same file it was loaded from?

If that's the question, then sure. When you load the data from a file, save the file's path in a variable somewhere. Then use that variable as the name of the file when you go to save the data.
 
nuala finnegan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Thanks very much for responding... Sorry the request is a bit crytic... I want the application to work like word.. So a user can open a file edit it and save it..
Or they can create a new file from scratch... If they edit an existing file i want them to be able to just hit save and I can do like you said to save the file name etc..

However, how do I prevent the dialog box from popping up? Like in for eg word you can just hit the save icon etc and nothing pops up...
This is my code, quite basic!!!

Thanks

Nuala

int returnVal1 = chooser.showSaveDialog(this);

if(returnVal1 == JFileChooser.APPROVE_OPTION) {

chooser.getSelectedFile().getPath());
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just remove that code. Remember, a JFileChooser is just what its name says, a thing for choosing a file. You already have the name of the file you want to save to, so you can skip the step which displays the JFileChooser and go directly to the code which saves the data in the file with that name.
 
nuala finnegan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh yeah (I'm being dim!)... thanks very much!!!
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean this works? I Will try it because I'm having similar challenge in my java app.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. You can read about file choosers here, but if you already have the file's details you don't need a chooser. I recommend you learn the newer ways to handle files, which you can read about under file I/O (NIO2).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic