简体   繁体   中英

Using Boolean True/False with JOptionPane.YES_NO_OPTION

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 (eg, 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 a 1 . 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 the public static final int YES_NO_OPTION default in 0.

Additionally, the line System.out.println(contactServerUp); comes back with an error that the field contactServerUp 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?

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);

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);

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM