Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Setting default name in JFileChooser

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a JFileChooser dialog, I want the initially displayed file name to be whatever the user chose last, or a default name if no file has been chosen yet. The following code correctly sets the current directory to whatever was selected last, but the file name field is always initially blank. Is there a way to make the dialog show a default file name?
void chooseTargetFile() {
JFileChooser chooser = new JFileChooser();
if (targetFile != null)
chooser.setSelectedFile(targetFile);
else chooser.setSelectedFile(newFile("default.xyz"));
int option = chooser.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
targetFile = chooser.getSelectedFile();
}
}
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gary,
Your code should do exactly that. Take a look at this simple test:

That pops up a Save file dialog in the working directory and dispalys default.xyz in the File name text field.
Michael Morris
 
Gary McGath
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code turns out to work fine in Windows. Apparently it's a bug in the Macintosh OS X implementation.
 
Your mother is a hamster and your father smells of tiny ads!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic