Nice programing

JOptionPane 예 / 아니요 옵션 확인 대화 상자 문제

nicepro 2020. 12. 10. 21:08
반응형

JOptionPane 예 / 아니요 옵션 확인 대화 상자 문제


나는 만들었고 JOptionPane두 개의 버튼 만 있습니다 YES_NO_OPTION.

JOptionPane.showConfirmDialog튀어 나온 를 클릭 YES BUTTON하여 계속 열고 JFileChooser싶습니다. 클릭하면 NO BUTTON작업이 취소됩니다.

꽤 쉬운 것 같지만 내 실수가 어디에 있는지 잘 모르겠습니다.

코드 스 니펫 :

if (textArea.getLineCount() >= 1) {  //The condition to show the dialog if there is text inside the textArea

    int dialogButton = JOptionPane.YES_NO_OPTION;
    JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);

    if (dialogButton == JOptionPane.YES_OPTION) { //The ISSUE is here

    JFileChooser saveFile = new JFileChooser();
    int saveOption = saveFile.showSaveDialog(frame);
    if(saveOption == JFileChooser.APPROVE_OPTION) {

    try {
        BufferedWriter fileWriter = new BufferedWriter(new FileWriter(saveFile.getSelectedFile().getPath()));
        fileWriter.write(textArea.getText());
        fileWriter.close();
    } catch(Exception ex) {

    }
}

에 대한 호출의 반환 값을 확인해야합니다 showConfirmDialog. IE :

int dialogResult = JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);
if(dialogResult == JOptionPane.YES_OPTION){
  // Saving code here
}

dialogButton대화 상자에 표시되어야하는 버튼을 설정하는 데 사용하던 에 대해 테스트 하고 있었으며이 변수는 업데이트 dialogButton되지 않았으므로 JOptionPane.YES_NO_OPTION.

Javadoc에 따라 showConfirmDialog:

반환 값 : 사용자가 선택한 옵션을 나타내는 정수


이 시도,

int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(this, "Your Message", "Title on Box", dialogButton);
if(dialogResult == 0) {
  System.out.println("Yes option");
} else {
  System.out.println("No Option");
} 

int opcion = JOptionPane.showConfirmDialog(null, "Realmente deseas salir?", "Aviso", JOptionPane.YES_NO_OPTION);

if (opcion == 0) { //The ISSUE is here
   System.out.print("si");
} else {
   System.out.print("no");
}

참고 URL : https://stackoverflow.com/questions/8689122/joptionpane-yes-no-options-confirm-dialog-box-issue

반응형