這種情況是,當選擇一個單選按鈕時,我打開一個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.
一個完整的例子可見[這裏](http://stackoverflow.com/a/4054307/230513)。 – trashgod 2014-11-06 19:54:31