日期:2014-05-20  浏览次数:20910 次

JFileChooser.showSaveDialog()设置默认文件名
JFileChooser.showSaveDialog()
怎么设置默认保存文件名?
------最佳解决方案--------------------
结贴吧。。调用它的方法setSelectedFile(new File("默认文件名"));

------其他解决方案--------------------
获取到JFileChooser的文件名的JTextField,然后对这个JTextField进行setText("abc");
JFileChooser没有特殊的API来获取这个JTextField,但是可以用通用的方式来获取到:

参数Container可以是任何Containter的实现子类,当然也包括JFileChooser。

public JTextField getTextField(Container c) {
JTextField textField = null;
for (int i = 0; i < c.getComponentCount(); i++) {
Component cnt = c.getComponent(i);
if (cnt instanceof JTextField) {
return (JTextField) cnt;
}
if (cnt instanceof Container) {
textField = getTextField((Container) cnt);
if (textField != null) {
return textField;
}
}
}
return textField;
}

------其他解决方案--------------------
1楼的方法肯定是有问题的,2楼的方法看上去好像可以,可是我试了没用啊!!请问还有没有其它的办法呢???
------其他解决方案--------------------
2楼的方法亲测可行。
在JFileChooser代码中声明,放在fchooser.showSaveDialog(fchooser)之前
JTextFiled text;
text=getTextFiled(fchooser);
text.setText("你想默认的文件名");

fchooser是JFileChooser fchooser = new JFileChooser();