반응형
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
반응형
'Nice programing' 카테고리의 다른 글
TextView의도 기호 (섭씨 / 화씨) (0) | 2020.12.10 |
---|---|
macosx에서 python matplotlib 프레임 워크? (0) | 2020.12.10 |
Rails : 특정 문자열을 포함하는 필드를 찾는 방법 (0) | 2020.12.10 |
동적 컬렉션과 함께 LINQ를 사용하는 방법 (0) | 2020.12.10 |
Ubuntu 14.04에서 1.8에서 1.9로 Ugrade tmux (0) | 2020.12.10 |