JFileChooser.showSaveDialog(...) - how to set suggested file name

yanchenko picture yanchenko · Dec 10, 2008 · Viewed 71.9k times · Source

The JFileChooser seems to be missing afeature: a way to suggest the file name when saving a file (the thing that usually gets selected so that it would get replaced when user starts typing).

Is there a way around this?

Answer

bruno conde picture bruno conde · Dec 10, 2008

If I understand you correctly, you need to use the setSelectedFile method.

JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setSelectedFile(new File("fileToSave.txt"));
jFileChooser.showSaveDialog(parent);

The file doesn't need to exist.

EDIT: If you pass a File with an absolute path, JFileChooser will try to position itself in that directory (if it exists).