Why does JOptionPane.getValue() continue to return uninitializedValue

孤街醉人 提交于 2019-12-08 10:45:15

问题


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


回答1:


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.




回答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
}


来源:https://stackoverflow.com/questions/4286679/why-does-joptionpane-getvalue-continue-to-return-uninitializedvalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!