gpt4 book ai didi

java - 如何让 jfilechooser 只接受 .txt

转载 作者:IT老高 更新时间:2023-10-28 21:01:56 26 4
gpt4 key购买 nike

我试图将我的联系人保存在我的表格中,但 filechosser 总是将其设置为所有文件。有没有办法可以将其设置为仅接受 .txt 并将其设为默认或唯一选项。

savecontact.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser filesave = new JFileChooser();
int returnVal = filesave.showSaveDialog(Main.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
File file = filesave.getSelectedFile();

PrintWriter os = new PrintWriter(file);
os.println("");
for (int col = 0; col < table.getColumnCount(); col++) {
os.print(table.getColumnName(col) + "\t");
}
os.println("");
os.println("");

for (int row = 0; row < table.getRowCount(); row++) {
for (int col = 0; col < table.getColumnCount(); col++) {
os.print(table.getColumnName(col));
os.print(": ");
os.println(table.getValueAt(row, col));
}
os.println("");
}
os.close();
System.out.println("Done!");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});

最佳答案

你需要添加一个过滤器:

JFileChooser jf = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
jf.setFileFilter(filter);

关于java - 如何让 jfilechooser 只接受 .txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15771949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com