0

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

Thang Pham
  • 38,125
  • 75
  • 201
  • 285

2 Answers2

3

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.

dogbane
  • 266,786
  • 75
  • 396
  • 414
2

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
}
josefx
  • 15,506
  • 6
  • 38
  • 63
  • Thank you very much, however dogbane answer first so I will mark his answer as the correct answer. Thank you again +1 – Thang Pham Nov 26 '10 at 16:45