【问题标题】:JFileChooser with confirmation dialog带有确认对话框的 JFileChooser
【发布时间】:2010-09-06 12:36:58
【问题描述】:

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

这个问题是关于 save 对话框的:new JFileChooser().showSaveDialog();。然后,用户可以在没有任何警告的情况下覆盖现有文件,这会是个问题。

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

提前致谢。

【问题讨论】:

    标签: java swing confirmation


    【解决方案1】:

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

    我希望这对其他人有用。

    【讨论】:

    • 帮帮我!我做了和你一样的事情,但是在用户选择一个选项后我无法关闭对话框。 “super.approveSelection()”帮助了我。谢谢
    • 对于showSaveDialog,您应该处理所选文件的扩展名。
    【解决方案2】:

    正如 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】:

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

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

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

      【讨论】:

        【解决方案4】:

        也许你可以验证文件不存在,甚至给 JFileChooser 一个FileSystemView(见this constructor)

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            猜你喜欢
            • 2015-06-26
            • 2012-04-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-06
            • 1970-01-01
            • 1970-01-01
            • 2016-02-19
            相关资源
            最近更新 更多