Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFileChooser save dialog without filename

 
Patrick Baril
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
saver.showSaveDialog(this);
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
Bartender
Posts: 12086
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
* JFileChooser.DIRECTORIES_ONLY
* JFileChooser.FILES_AND_DIRECTORIES



 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic