File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes JFileChooser save dialog without filename Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JFileChooser save dialog without filename" Watch "JFileChooser save dialog without filename" New topic

JFileChooser save dialog without filename

Patrick Baril

Joined: Mar 18, 2011
Posts: 2
Hello all,

In my little program, user can save there data base. I use JFileChooser to open dialog box. here is my code :

JFileChooser saver = new JFileChooser();
saver.setDialogTitle("Select destination");
File file = saver.getCurrentDirectory();

It works fine, but the problem is that user must enter a filename for it to work, and I don't care about a filename. In my program, saved filename are hardcoded and always have the same name so I only care about the path.

Is there a way to open a JFileChooser but without a filename field, but where user can browse to the desired directory and hit save?

Thank you!
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

according to the api, there is a setFileSelectionMode method, that lets you select how it works:

mode - the type of files to be displayed:

* JFileChooser.FILES_ONLY

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46360
There are bound to be methods in JFileChooser allowing you to set the selected directory or the selected file. Have a look and see what you can find.
Edwin Torres
Ranch Hand

Joined: Mar 19, 2011
Posts: 55

Try adding these two lines:

The first line tells the chooser that we only want to select directories. The second line disables the accept all file filter.

Twitter: @realEdwinTorres
Blog: java Friendly
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46360
You can probably get the name of the directory, which comes back as a String, and append the file name to it with the + operator.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

You get a File object back from getSelectedFile(), which represents a directory in this case. You should then use the File constructor that takes a parent File.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: JFileChooser save dialog without filename
jQuery in Action, 3rd edition