代码之家  ›  专栏  ›  技术社区  ›  Roberto Luis Bisbé

带确认对话框的JFileChooser

  •  36
  • Roberto Luis Bisbé  · 技术社区  · 15 年前

    我正在开发一个从文本文件加载和保存数据的程序,我在加载和保存时向用户询问jfilechooser的文件名。

    这个问题是关于 节约 对话: new JFileChooser().showSaveDialog(); . 然后,用户可以在不发出任何警告的情况下覆盖现有文件,并且 会是个问题 .

    关于如何解决这个问题有什么建议吗?我一直在寻找一些方法或选择,但没有找到任何东西。

    事先谢谢。

    5 回复  |  直到 12 年前
        1
  •  76
  •   Radu Simionescu    12 年前

    感谢您的回答,但我找到了另一个解决方法,覆盖了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();
        }        
    }
    

    我希望这对其他人有用。

        2
  •  4
  •   kmindi    14 年前

    正如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
  •  3
  •   Jigar Joshi    15 年前

    保存前检查是否已存在相同的文件,然后请求用户确认她是否确实要覆盖: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码

    要检查文件是否已存在,请使用

    boolean exists = (new File("filename")).exists();
    if (exists) {
        // File or directory exists
    } else {
        // File or directory does not exist
    }
    
        4
  •  1
  •   Riduidel    15 年前

    也许您可以验证文件是否已经存在,甚至给jfilechooser一个 FileSystemView (见 this constructor )

        5
  •  1
  •   matt burns    14 年前

    我是根据你自己的答案写的。发布以防其他人发现它有用:

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