Search code examples
javaswingjoptionpane

Why does JOptionPane.getValue() continue to return uninitializedValue


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.