I trying to save my contact in my table but filechosser always setit to all file. is there way I can set it to accept .txt only and make it default or the only option.
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 provides a simple mechanism for the user to choose a file. For information about using JFileChooser , see How to Use File Choosers, a section in The Java Tutorial.
public final class FileNameExtensionFilter extends FileFilter. An implementation of FileFilter that filters using a specified set of extensions. The extension for a file is the portion of the file name after the last ".". Files whose name does not contain a "." have no file name extension.
You need to add a filter:
JFileChooser jf = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text"); jf.setFileFilter(filter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With