JFileChooser with confirmation dialog

前端 未结 5 891
忘掉有多难
忘掉有多难 2020-12-08 00:38

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

相关标签:
5条回答
  • 2020-12-08 01:06

    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;
        }
    });
    
    0 讨论(0)
  • 2020-12-08 01:12

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

    0 讨论(0)
  • 2020-12-08 01:16

    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.

    0 讨论(0)
  • 2020-12-08 01:23

    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();
            }
        };
    
    0 讨论(0)
  • 2020-12-08 01:23

    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
    }
    
    0 讨论(0)
提交回复
热议问题