Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I make jfilechooser only accept .txt

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();             }         }     } }); 
like image 407
Devendra Danny Ramdayal Avatar asked Apr 02 '13 18:04

Devendra Danny Ramdayal


People also ask

What is JFileChooser in Java?

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.

What is FileNameExtensionFilter in Java?

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.


1 Answers

You need to add a filter:

JFileChooser jf = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text"); jf.setFileFilter(filter); 
like image 146
Marcelo Tataje Avatar answered Sep 22 '22 18:09

Marcelo Tataje