Why does JOptionPane.getValue() continue to return uninitializedValue
-
28-09-2019 - |
Question
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
Solution
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.
OTHER TIPS
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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow