Joptionpane.showinputdialog no cancel button

Author: Rainer Date: 23.06.2017

I am using an Input Dialog for user input. The input can be a string value, or simply pressing "OK" to use the default value.

java - Is there a way to only have the OK button in a JOptionPane showInputDialog (and no CANCEL button)? - Stack Overflow

With this, there is no way to differentiate between the user specifying the default value no string input, "OK" button press and the user canceling "Cancel" button press. Both return a null String. It looks like I can register a PropertyChangeListener on the InputDialog, but that requires apparently constructing the InputDialog directly, then adding to a JDialog, etc.

Is there some other way to differentiate between "OK" with a null value and "Cancel" without having to build the InputDialog myself and add the PropertyChangeListener? You could use "" for the initialSelectionValue parameter to differentiate between OK and Cancel:.

If the user clicked Cancel, s is null, if the user clicked OK and did not enter a value, it is "". By posting your answer, you agree to the privacy policy and terms of service. Stack Overflow Questions Developer Jobs Documentation beta Tags Users.

java - How to handle cancel button in JOptionPane - Stack Overflow

Sign up or log in to customize your list. Tour Start here for fidelity stock trading joptionpane.showinputdialog no cancel button overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up. Join the Stack Overflow Community.

OK cancel option dialog : JOptionPane Dialog « Swing « Java Tutorial

Stack Overflow is a community of 7. Join them; it only takes a minute: Differentiating between no input null String and Cancel button in JOptionPane. It doesn't matter if I set an initial value or not. Post your SSCCE that demonstrates the problem.

Thus, differentiating is easy as long as I don't use the offending package. Your test case did lead me to the actual problem, thanks. You could use "" for the initialSelectionValue parameter to differentiate between OK and Cancel: I get an empty string even if I don't set an initial value and just press OK. Did you use the same version of showInputDialog? I'm saying I used two versions of showInputDialog, one with an initial value and one without.

In both cases an empty string was returned.

Java prog#125. JOptionPane: Other ways of Using JOptionPane for showing variable values

In other words I could not duplicate the problem because I never got "null" when I click Ok. So I am questioning the problem statement of the OP. Sign up or log in StackExchange. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. Stack Overflow works best with JavaScript enabled. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3. Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

joptionpane.showinputdialog no cancel button
Rating 4,5 stars - 917 reviews
inserted by FC2 system