How to set a default file name to Swing JFileChooser?
Asked Answered
A

2

7

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

Abridge answered 15/11, 2012 at 15:26 Comment(2)
Hi Chirag Soni, take a look at this If you already have a code, please post code snippet :) References from our site StackOverflowNippon
possible duplicate of JFileChooser.showSaveDialog(...) - how to set suggested file nameTenement
M
9

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

Milliary answered 15/11, 2012 at 16:0 Comment(1)
C:/ How would that work on Linux/Unix or OS X? Better to point the user towards user.home.Internuncio
O
5

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

Oligosaccharide answered 23/6, 2014 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.