문제

Below are my code

public static void main(String args[]){
     JOptionPane pane = new JOptionPane();
     pane.showInputDialog(null, "Question");
     Object value = value.getValue();
     System.out.println(value.toString()); --> this will print out uninitializedValue

}

I basically want to detect when the user click the cancel in JOptionPane and when the user close the JOptionPane

도움이 되었습니까?

해결책

You should do this:

    String s = JOptionPane.showInputDialog(null, "Question");
    System.out.println(s);

This will return a null string if the pane is closed or Cancel is pressed.

다른 팁

showInputDialog is a static method, it does not modify the JOptionPane. As dogbane points out you should check the return value showInputDialog.

Some compilers generate warnings if you call static methods on instances, so always check compiler warnings. In your case call the method like this:

String result = JOptionPane.showInputDialog(null, "Question");
if(result == null){
//chancel pressed
}else{
//normal code
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top