2014-11-06 1 views
2

시나리오는 라디오 버튼을 선택하면 JFileChooser를 열어 DIRECTORY를 선택해야합니다. 오류 메시지를 표시하려고하는데 디렉토리 선택기를 다시 표시하려고합니다. 이 코드 (I 전화 기능 때 라디오 버튼 변경)입니다 :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.

답변

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

이렇게하면 파일 선택기가 닫히지 않고 다시 열리지 않지만 필요한 조건이 충족되거나 취소가 수행 될 때까지 열려 있습니다.

또한 null이 아닌 JOptionPane에 대해 parentComponent을 제공해야합니다. 그들은 더 말할 경우

+0

완전한 예를 볼 수있다 [여기] (http://stackoverflow.com/a/4054307/230513). – trashgod

0

는 JFileChooser를 &위한 JOptionPane 주위에 do { } while 루프를 사용하지 않고, 대신 오류 메시지의, 같은 메시지 표시 "을 (를) 찾을 수 없습니다 필요한 파일을. 다른 디렉토리를 선택 하시겠습니까?", JOptionPane.YES_NO_OPTION를 사용하고 종료

+0

고맙습니다.하지만이 기능을 재귀 적으로 호출하는 것은 그리 멀지 않은 방법입니다. 그리고 나는 스택 오류 문제를 가질 수 있다고 생각합니다 ... – Francesco

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

+0

호출 스택에 문제가 없습니다. –

관련 문제