• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

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: 13053
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: 75836
361
  • 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: 75836
361
  • 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: 22656
126
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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic