2009-01-30 7 views
0

아주 간단한 텍스트 편집기를 작성 중이며 사소한 문제가 있습니다. 코드는 아래와 같습니다.특정 대화 상자로 돌아 가기

파일을 저장하면 파일이 존재할 때 덮어 쓰거나 취소하거나 다시 쓰지 않을 것인지 묻는 메시지가 표시됩니다 (다시 시도 할 수있는 옵션 있음). 그것은 JFileChoose 대화 상자로 반환해야합니다, 예, 아니오, 선택되는 어떠한의 경우

을 취소,하지만 난 그냥 평범한 모른다 :

은 그래서 덮어하라는 메시지를 표시합니다 JFileChooser를이 방법. 아무도 나를 도울 수 있습니까? (지금까지 내가 알고 있어요으로, 깊이 테스트하지 않은)을 취소 그래 옵션은 문제가되지 않습니다

String contents = textArea.getText(); 
if (openPath != null) { 
    savePath = openPath;      
} else if (saveAsPath != null) { 
    savePath = saveAsPath; 
} else if (savePath != null) { 
    savePath = savePath; 
} else if (openPath == null) { 
    JFileChooser saveFile = new JFileChooser(); 
    int returnVal = saveFile.showOpenDialog(null); 
    if (returnVal == saveFile.APPROVE_OPTION) { 
     savePath = saveFile.getSelectedFile(); 
     if (!savePath.exists()) { 
     FileWriter fstream = new FileWriter (savePath); 
     BufferedWriter saveWrite = new BufferedWriter(fstream); 
     saveWrite.write(contents); 
     saveWrite.close(); 
     } else if (savePath.exists()) { 
      JOptionPane overwritePrompt = new JOptionPane(); 
      Object[] options = {"Yes","No","Cancel"}; 
      int n = JOptionPane.showOptionDialog(overwritePrompt, 
       "Already exists. Overwrite?","Overwrite File?", 
       JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE, 
       null,options,options[2]); 
     if (n == 0) { 
     FileWriter fstream = new FileWriter(saveAsPath); 
     BufferedWriter out = new BufferedWriter(fstream); 
     out.write(contents); 
     out.close(); 
     } else if (n == 1) { 
      savePath = null; 
      openPath = null; 
      saveAsPath = null; 
      return; // should return to JFileChooser 
     } else { 
       savePath = null; 
      openPath = null; 
      saveAsPath = null; 
      return; 
     } 
    } 
} else { 
    return; 
} 

답변

0

어떤이 라인에서 처음부터 다시 약 : 다시

int returnVal = saveFile.showOpenDialog(null); 

, JFileChooser로 돌아 가야 할 때? 나는 같은 파일 선택 도구 이 열린 대화 상자를 다시 표시한다고 가정합니까?

+0

은 와우, 짜증. 감사합니다. 코드를 파괴하는 것이 끔찍한 일이라고 느끼지만, 당신은 죽었습니다. 나는 JChooseFile을별로 ​​보지 않았다. –