JFileChooser with confirmation dialog

2019-01-21 13:31发布

I am working on a program that loads and saves data from text files, and I am asking the user a file name with JFileChooser on load and save.

This question is about the save dialog: new JFileChooser().showSaveDialog();. The user then could overwrite an existing file without any warning, and that would be a problem.

Any suggestion on how to fix this? I have been looking for some method or option, but I didn't found anything.

Thanks in advance.

5条回答
狗以群分
2楼-- · 2019-01-21 13:37

Thanks for the answers, but I found another workaround, overriding the approveSelection() of the JFileChooser, this way:

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();
    }        
}

I hope this could be useful for someone else.

查看更多
一夜七次
3楼-- · 2019-01-21 13:38

I wrote this based on your own answer. Posted in case someone else finds it useful:

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;
    }
});
查看更多
The star\"
4楼-- · 2019-01-21 13:44

As AvrDragon said, closing with X is not handled. I added a default case to handle all unrelevant options:

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();
        }
    };
查看更多
何必那么认真
5楼-- · 2019-01-21 13:48

Check before saving if the same file already exist then ask user for confirmation does she really want to override :p

 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");
    }  

here is code

To check file already exist use

boolean exists = (new File("filename")).exists();
if (exists) {
    // File or directory exists
} else {
    // File or directory does not exist
}
查看更多
孤傲高冷的网名
6楼-- · 2019-01-21 13:55

Maybe you could verify that the file does not already exists, and even give the JFileChooser a FileSystemView (see this constructor)

查看更多
登录 后发表回答