How to set default file name for Swing JFileChooser?

I want to set the default file name as Untitled.txt in the text box of this JFileChooser . Can i install this?

+6
source share
2 answers

Use the following code:

  JFileChooser fileChooser = new JFileChooser(); File file = new File("C:/untitled.txt"); fileChooser.setCurrentDirectory(file); 

You must specify the full path to untitled.txt

+8
source

You need to use the setSelectedFile method and pass the file as a parameter. The file must not exist.
If the file path is specified, the file selection will try to point to the file directory, if one exists

JFileChooser fileChooser = new JFileChooser(); fileChooser.setSelectedFile(new File("C:\\file.txt")); fileChooser.showSaveDialog(null);

+4
source

All Articles