How to set a default file name to Swing JFileChooser?

Chirag Soni picture Chirag Soni · Nov 15, 2012 · Viewed 16.8k times · Source

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

Answer

Gilbert Le Blanc picture Gilbert Le Blanc · Nov 15, 2012

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