Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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) + "");
                }
                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();
            }
        }
    }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
177 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to add a filter:

JFileChooser jf = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
jf.setFileFilter(filter);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...