JFileChooser带有确认对话框

48
我正在开发一个程序,用于从文本文件中加载和保存数据。在加载和保存时,我使用JFileChooser向用户请求文件名。
这个问题涉及到“保存”对话框:new JFileChooser().showSaveDialog();。用户有可能无意间覆盖已存在的文件,这将是一个问题。
你有什么建议可以解决这个问题吗?我一直在寻找某种方法或选项,但是没有找到任何东西。
提前感谢您的回答。
5个回答

92

谢谢回答,但我找到了另一个解决方法,重写JFileChooser的approveSelection()方法,如下:

JFileChooser example = new JFileChooser(){
    @Override
    public void approveSelection(){
        File f = getSelectedFile();
        if(f.exists() && getDialogType() == SAVE_DIALOG){
            int result = JOptionPane.showConfirmDialog(this,"The file exists, overwrite?","Existing file",JOptionPane.YES_NO_CANCEL_OPTION);
            switch(result){
                case JOptionPane.YES_OPTION:
                    super.approveSelection();
                    return;
                case JOptionPane.NO_OPTION:
                    return;
                case JOptionPane.CLOSED_OPTION:
                    return;
                case JOptionPane.CANCEL_OPTION:
                    cancelSelection();
                    return;
            }
        }
        super.approveSelection();
    }        
}

我希望这对其他人有所帮助。


1
帮了我大忙!我做了和你一样的事情,但是在用户选择选项后关闭对话框方面遇到了麻烦。"super.approveSelection()" 帮了我大忙。谢谢! - prolink007
在showSaveDialog中,您应该处理所选文件的扩展名。 - onurbaysan

7
正如AvrDragon所说,以X结尾的情况未被处理。我添加了一个默认情况,以处理所有不相关的选项:
final JFileChooser fc = new JFileChooser() {

        private static final long serialVersionUID = 7919427933588163126L;

        public void approveSelection() {
            File f = getSelectedFile();
            if (f.exists() && getDialogType() == SAVE_DIALOG) {
                int result = JOptionPane.showConfirmDialog(this,
                        "The file exists, overwrite?", "Existing file",
                        JOptionPane.YES_NO_CANCEL_OPTION);
                switch (result) {
                case JOptionPane.YES_OPTION:
                    super.approveSelection();
                    return;
                case JOptionPane.CANCEL_OPTION:
                    cancelSelection();
                    return;
                default:
                    return;
                }
            }
            super.approveSelection();
        }
    };

3

在保存之前检查是否存在相同的文件,然后询问用户确认是否要覆盖:

 JDialog.setDefaultLookAndFeelDecorated(true);
    int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to override existing file?", "Confirm",
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (response == JOptionPane.NO_OPTION) {
      System.out.println("No button clicked");
    } else if (response == JOptionPane.YES_OPTION) {
      System.out.println("Yes button clicked");
    } else if (response == JOptionPane.CLOSED_OPTION) {
      System.out.println("JOptionPane closed");
    }  

这里需要翻译的内容是:

这里有相关代码

要检查文件是否已经存在,请使用以下代码:

boolean exists = (new File("filename")).exists();
if (exists) {
    // File or directory exists
} else {
    // File or directory does not exist
}

1

我根据你的答案编写了这篇文章。发布出来,以便其他人也能从中受益:

final JFileChooser exportFileChooser = new JFileChooser();
exportFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
exportFileChooser.setApproveButtonText("Export");

final JButton exportButton = new JButton("Export text file");
exportButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        int returnVal = exportFileChooser.showSaveDialog(exportButton
                .getParent());

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File outputFile = exportFileChooser.getSelectedFile();
            if (outputFileIsValid(outputFile)) {
                exportFile(outputFile);
            }
        }
    }

    private boolean outputFileIsValid(File outputFile) {
        boolean fileIsValid = false;
        if (outputFile.exists()) {
            int result = JOptionPane.showConfirmDialog(
                    exportButton.getParent(),
                    "File exists, overwrite?", "File exists",
                    JOptionPane.YES_NO_CANCEL_OPTION);
            switch (result) {
            case JOptionPane.YES_OPTION:
                fileIsValid = true;
                break;
            default:
                fileIsValid = false;
            }
        } else {
            fileIsValid = true;
        }
        return fileIsValid;
    }
});

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接