2014-11-06 50 views
2

这种情况是,当选择一个单选按钮时,我打开一个JFileChooser来选择一个目录,其中应该是某些文件。 我试图显示错误消息,我想再次显示目录选择器。 这是代码(我称之为功能时单选按钮的变化):如何在JFileChooser OK按下后退出?

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.

回答

5

当创建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

+0

一个完整的例子可见[这里](http://stackoverflow.com/a/4054307/230513)。 – trashgod 2014-11-06 19:54:31

0

使用do { } while循环在你的JFileChooser &的JOptionPane,而不是一个错误信息,显示一个类似的消息“找不到必需的文件。你想选择一个不同的目录?”,使用​​并退出,如果他们说不

+0

谢谢,但这是递归调用函数并不遥远。 和我想我可以有堆栈错误问题... – Francesco 2014-11-07 14:47:22

0

尝试下一:

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

谢谢,但这是递归调用函数不远。 ,我想我可以有堆栈错误问题... – Francesco 2014-11-07 14:47:56

+0

你不会有调用堆栈的问题。 – 2014-11-07 15:17:39

相关问题