我试图将我的联系人保存在我的表中但是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(); …Run Code Online (Sandbox Code Playgroud)