2014-11-06 54 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

相關問題