How to set a default file name to Swing JFileChooser?
17,252
Solution 1
Use the following code:
JFileChooser fileChooser = new JFileChooser();
File file = new File("C:/untitled.txt");
fileChooser.setCurrentDirectory(file);
You have to specify the complete path to untitled.txt
Solution 2
You have to use the setSelectedFile method and pass a file as a parameter. The file doens't have to exist.
If the path to the file is specified the file chooser will try to point to the file directory if it exists
JFileChooser fileChooser = new JFileChooser();
fileChooser.setSelectedFile(new File("C:\\file.txt"));
fileChooser.showSaveDialog(null);
Related videos on Youtube
Author by
Chirag Soni
A software Engineer working mainly in JAVA technologies. Also have knowledge about JavaScript frameworks like AngularJs, nodeJs and ExtJS.
Updated on September 15, 2022Comments
-
Chirag Soni about 1 year
I want to set a default file name as
Untitled.txt
in text-box of thisJFileChooser
. Can I set this?-
Math about 10 yearspossible duplicate of JFileChooser.showSaveDialog(...) - how to set suggested file name
-
-
Andrew Thompson almost 11 years
C:/
How would that work on Linux/Unix or OS X? Better to point the user towardsuser.home
.