aspose file tools*
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
Author

JFileChooser save dialog without filename

Patrick Baril
Greenhorn

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");
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

Joined: Oct 02, 2003
Posts: 11401
    
  16

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




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

Joined: Oct 13, 2005
Posts: 39388
    
  28
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
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFileChooser save dialog without filename