【问题标题】:How not to exit after JFileChooser OK pressed?按下 JFileChooser OK 后如何不退出?
【发布时间】:2014-11-06 17:25:45
【问题描述】:

场景是,当一个单选按钮被选中时,我打开一个 JFileChooser 来选择一个应该是一些文件的目录。 我正在尝试显示一条错误消息,并且我想再次显示目录选择器。 这是代码(单选按钮更改时我调用的函数):

private void JFileChooserOpen(java.awt.event.ActionEvent evt) {
    fileChooser.setCurrentDirectory(new java.io.File("."));
    fileChooser = new JFileChooser(); 
    fileChooser.setDialogTitle("Select a directory");
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    fileChooser.setAcceptAllFileFilterUsed(false);

    int result = fileChooser.showOpenDialog(fileChooser);
    if (result == JFileChooser.APPROVE_OPTION) {
// here I'm calling a function that searches for specific files. 
// true these files are found, false, they are not.
        if (checkTheDir(fileChooser.getSelectedFile()))
        {
// assigning the path to a label
                thePath.setText(fileChooser.getSelectedFile().toString());
        }
        else
        {
// file not found
            JOptionPane.showMessageDialog(null, "File GuiRap not found!", 
                    "Controlla meglio", JOptionPane.WARNING_MESSAGE);
// what should I do, here, to open again the file dialog window?
// here I'm calling again this function, but surely is not a good practice!
            JFileChooserOpen(evt);
        }
// cancel button changes a radiobutton
    } else if (result == JFileChooser.CANCEL_OPTION) {
        rbnParams.setSelected(true);
    }
} 

非常感谢! F.

【问题讨论】:

    标签: java swing jfilechooser


    【解决方案1】:

    创建JFileChooser 时,覆盖其approveSelection 方法(documentation)。

    JFileChooser fileChooser = new JFileChooser() {
        @Override
        public void approveSelection() {
            // test your condition here
            if (checkTheDir(getSelectedFile())
                super.approveSelection();
            else
                JOptionPane.showMessageDialog(null, "File GuiRap not found!", 
                    "Controlla meglio", JOptionPane.WARNING_MESSAGE);                
        }
    }
    

    这样,文件选择器不会关闭并重新打开,而是保持打开状态,直到满足所需条件(或执行取消)。

    此外,您应该为该JOptionPane 提供parentComponent,而不是提供null

    【讨论】:

      【解决方案2】:

      在您的 JFileChooser 和 JOptionPane 周围使用 do { } while 循环,而不是错误消息,而是显示一条消息,例如“找不到必要的文件。您要选择其他目录吗?”,使用 JOptionPane.YES_NO_OPTION 并在以下情况下退出他们说不

      【讨论】:

      • 谢谢,但这离递归调用函数不远了。而且我认为我可能会遇到堆栈错误问题...
      【解决方案3】:

      尝试下一个:

      private void JFileChooserOpen(java.awt.event.ActionEvent evt) {
          fileChooser = new JFileChooser(); 
          fileChooser.setDialogTitle("Select a directory");
          fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
          fileChooser.setAcceptAllFileFilterUsed(false);
          boolean valid = false;
          do {
              fileChooser.setCurrentDirectory(new java.io.File("."));
              if (fileChooser.showOpenDialog(fileChooser) == JFileChooser.APPROVE_OPTION) {
                  valid = checkTheDir(fileChooser.getSelectedFile());
                  if (valid) {
                      thePath.setText(fileChooser.getSelectedFile().toString());
                  } else {
                      JOptionPane.showMessageDialog(null, "File GuiRap not found!", 
                          "Controlla meglio", JOptionPane.WARNING_MESSAGE);
                  }
              } else {
                  rbnParams.setSelected(true);
                  valid = true;
              }
          } while(!valid);
      } 
      

      【讨论】:

      • 谢谢,但这离递归调用函数不远了。而且我认为我可能会遇到堆栈错误问题...
      • 调用堆栈不会有问题。
      猜你喜欢
      • 2019-04-07
      • 2012-10-22
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 2019-10-29
      • 2023-03-29
      • 2021-04-16
      • 2020-07-20
      相关资源
      最近更新 更多