Search code examples
javaswingjoptionpane

JOptionPane.showInputDialog()


So I have the method JOptionPane.showInputDialog() which returns a String that is the value input by the user. The Dialog box has an OK and a Cancel button. How can I check so that I know whether the user clicked OK or Cancel?


Solution

  • JOptionPane.showInputDialog() will return the string the user has entered if the user hits ok, and returns null otherwise. Therefore, you can just check to see if the resultant string is null. The javadocs on the method here are not terribly explicit about this, but where the return behavior is mentioned, it is indicated to be so.