Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default file name to Swing JFileChooser?

I want to set a default file name as Untitled.txt in text-box of this JFileChooser. Can I set this?

like image 449
Chirag Soni Avatar asked Nov 15 '12 15:11

Chirag Soni


2 Answers

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

like image 73
Gilbert Le Blanc Avatar answered Nov 12 '22 01:11

Gilbert Le Blanc


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);

like image 31
pedromendessk Avatar answered Nov 12 '22 01:11

pedromendessk