• 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 dialog without filename

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 57
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic