39

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();
            }
        }
    }
});
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

3 Answers3

92

You need to add a filter:

JFileChooser jf = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
jf.setFileFilter(filter);
Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51
  • the code work but I still have to type in .txt extension to the end of the file. is there a way i can save it as .txt by default – Devendra Danny Ramdayal Apr 02 '13 at 19:28
  • 1
    After calling `File file = filesave.getSelectedFile();`, you can check whether the filename has the extension "txt". If not, you create a new File object containing the extension and then continue with `new PrintWriter(file2);` (Creating a new `File` object doesn't actually create the file.) – Enwired Apr 02 '13 at 20:59
  • @Enwired By the look of the code - saving a table with `\t` delimiters for values in a row and new lines for rows, it should really be called `.tsv` (tab separated variable)! – Andrew Thompson Apr 03 '13 at 01:44
  • @AndrewThompson I agree in principle, but the question asker wanted to know how to force the file extension `.txt`. I simply stated one way to do that. He may have some reason why he prefers `.txt` over `.tsv`, and that is none of my business. For example, Windows machines don't generally recognize the extension `.tsv`, though they do recognize `.csv`. – Enwired Apr 03 '13 at 17:42
  • I was able to figure it after messing around with the code. thank for the help – Devendra Danny Ramdayal Apr 06 '13 at 01:52
23

Here some examples

fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("*.pdf", "pdf"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("*.txt", "txt"));
alexandre-rousseau
  • 2,321
  • 26
  • 33
Nilesh Jadav
  • 890
  • 9
  • 8
4

You could do that by using FileFilter.

Create a Filefilter with the necessary conditions. Set this file filter to the JFileChooser, and launch it.

syb0rg
  • 8,057
  • 9
  • 41
  • 81
Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68