aspose file tools
The moose likes Beginning Java and the fly likes JfileChooser save without a dialog box popping up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JfileChooser save without a dialog box popping up" Watch "JfileChooser save without a dialog box popping up" New topic
Author

JfileChooser save without a dialog box popping up

nuala finnegan
Greenhorn

Joined: Jun 15, 2012
Posts: 5
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
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19676
    
  10

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
Sheriff

Joined: Oct 14, 2005
Posts: 19676
    
  10

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

Joined: Jun 15, 2012
Posts: 5
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
Sheriff

Joined: Oct 14, 2005
Posts: 19676
    
  10

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

Joined: Jun 15, 2012
Posts: 5
oh yeah (I'm being dim!)... thanks very much!!!
Joe Don
Ranch Hand

Joined: Jun 01, 2012
Posts: 47
You mean this works? I Will try it because I'm having similar challenge in my java app.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43295
    
  32
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).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JfileChooser save without a dialog box popping up
 
It's not a secret anymore!