how do I make jfilechooser only accept .txt
Asked Answered
G

3

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();
            }
        }
    }
});
Gyration answered 2/4, 2013 at 18:47 Comment(0)
G
92

You need to add a filter:

JFileChooser jf = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
jf.setFileFilter(filter);
Gelsenkirchen answered 2/4, 2013 at 18:51 Comment(5)
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 defaultGyration
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.)Auroreaurous
@Auroreaurous 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)!Isomeric
@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.Auroreaurous
I was able to figure it after messing around with the code. thank for the helpGyration
R
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"));
Rosina answered 4/10, 2014 at 9:7 Comment(1)
is this will add all of the extension without removing the earlier filter added?Underbelly
I
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.

Irksome answered 2/4, 2013 at 18:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.