小编Dev*_*yal的帖子

如何让jfilechooser只接受.txt

我试图将我的联系人保存在我的表中但是filechosser总是设置为所有文件.有没有办法我可以设置它只接受.txt并使其成为默认或唯一选项.

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(); …
Run Code Online (Sandbox Code Playgroud)

java file-io swing jfilechooser

31
推荐指数
2
解决办法
6万
查看次数

标签 统计

file-io ×1

java ×1

jfilechooser ×1

swing ×1