Using Boolean True/False with JOptionPane.YES_NO_OPTION
Solution 1
According to the javadoc, when one of the showXxxDialog methods returns an integer, the possible values are:
- YES_OPTION
- NO_OPTION
- CANCEL_OPTION
- OK_OPTION
- CLOSED_OPTION
You should test against those constants:
contactServerUp = (contactServerEntry == JOptionPane.YES_OPTION);
Solution 2
The value returned by the the JOptionPane dialogs are values defined as constant fields in the class.
Although, indeed, one could assume that 0 means false and 1 means true, the values are more ids for the different buttons a dialog can have.
To know if the user pressed yes or no, you can compare the return value to the constant fields described here. For example, in your case :
contactServerUp = (contactServerEntry == JOptionPane.YES_OPTION);
Solution 3
Since a dialog a JOptionPane can have more than two possible 'answers' a boolean would be a poor representation. You are forgetting about the YES, NO and CANCEL option, or what about just a OK answer.
If it would have been written today, I suspect a Enum would have been used instead of an int.
Solution 4
As for the second question, the compiler does not allow access to uninitialized variables. When you do the following, there is a chance that the variable might not be initialized:
if(contactServerEntry==1)
contactServerUp = true;
else
if(contactServerEntry==0)
contactServerUp = false;
What if, for example, contactServerEntry == JOptionPane.CLOSED_OPTION
? In that case, your boolean value is never initialized.
You need to add an else
clause at the end of your if-else chain, or initialize contactServerUp value to a default value in the beginning.
dwwilson66
Visual Communications Professional with a background in technology. In the thick of learning Java, PHP & MySQL to augment my web skills. I'm taking a shine to programming a lot more than I thought I would.
Updated on June 04, 2022Comments
-
dwwilson66 about 2 years
I have a series of four yes/no choices in four separate dialog boxes, the cumulative results of which will lead to one of twelve separate links (e.g., Yes/Yes/Yes/No -> link A, Yes/No/No/Yes -> link B, etc). The branching logic uses boolean values.
Here's what I have so far...just the first dialog box and printing the results for validation.
public class OutageGuideSelector{ public static void main(String[] args){ boolean contactServerUp; boolean vistaUp; boolean stormOutage; boolean vistaCSUp; // int contactServerEntry = JOptionPane.showConfirmDialog(null, "Is the contact server up", "Please select", JOptionPane.YES_NO_OPTION); System.out.println("result from entry " + contactServerEntry); if(contactServerEntry==1) contactServerUp = true; else if(contactServerEntry==0) contactServerUp = false; /* System.out.println(contactServerUp); */ }}
Right now, the results of clicking YES reults in a
0
being returned, NO results in a1
. Is this normal, seems counterintuitive, and there's nothing at docs.oracle.java that shows a clear example of the output values except this which seems to suggest that thepublic static final int YES_NO_OPTION
default in 0.Additionally, the line
System.out.println(contactServerUp);
comes back with an error that the fieldcontactServerUp might not have been initialized
when I un-comment it, so I can't see if my convert-int-to-boolean is working.First: It appears that JOptionPane method does not include any boolean returns...except
getWantsInput()
which returns the value of the wantsInput property...so I assume I'm already being the most efficient I can with this. I'd like to know if there's an easier way.Second, what am I missing that prevents my console output statement from recognizing the
contactServerUp
? Where's my misplaced semicolon?