java 如何将默认文件名设置为 Swing JFileChooser?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13400656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a default file name to Swing JFileChooser?
提问by Chirag Soni
I want to set a default file name as Untitled.txt
in text-box of this JFileChooser
. Can I set this?
我想Untitled.txt
在 this 的文本框中设置一个默认文件名JFileChooser
。我可以设置这个吗?
回答by Gilbert Le Blanc
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
您必须指定完整路径 untitled.txt
回答by RageAgainstTheMachine
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
您必须使用 setSelectedFile 方法并将文件作为参数传递。该文件不必存在。
如果指定了文件路径,文件选择器将尝试指向文件目录(如果存在)
JFileChooser fileChooser = new JFileChooser();
fileChooser.setSelectedFile(new File("C:\\file.txt"));
fileChooser.showSaveDialog(null);
JFileChooser fileChooser = new JFileChooser();
fileChooser.setSelectedFile(new File("C:\\file.txt"));
fileChooser.showSaveDialog(null);