Prev: JOptionPane - Simple Dialogs | Next: none

# Java: JOptionPane.showOptionDialog

Here are some more useful static methods from `javax.swing.JOptionPane` that allow you to ask the user to indicate a choice.

Value | Method call
`response =` | `JOptionPane.showConfirmDialog(component, text);`
`response =` | `JOptionPane.showOptionDialog(component, message, title, optionType, messType, icon, options, initVal);`

## Example

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
    // File       : joptionpane-example/JOptionPaneTest2.java
    // Description: JOptionPane.showOptionDialog demonstration.
    // Illustrates: showOptionDialog and switch.
    // Author     : Fred Swartz, 16 Apr 2007, Placed in public domain.

    import javax.swing.JOptionPane;

    public class JOptionPaneTest2 {
        public static void main(String[] args) {
            //... Text to put on the buttons.
            String[]  choices = {
                "Democratic",
                "Republican",
                "None of your business",
                "Quit"};

            //... Variables to keep track of the counts.
            int democraticCount = 0;
            int republicanCount = 0;
            int noAnswerCount   = 0;

            //... "Infinite" loop, terminated by call to System.exit(0)
            while (true) {
                int response = JOptionPane.showOptionDialog(
                                   null                       // Center in window.
                                 , "How did you vote?"        // Message
                                 , "Party Poll"               // Title in titlebar
                                 , JOptionPane.YES_NO_OPTION  // Option type
                                 , JOptionPane.PLAIN_MESSAGE  // messageType
                                 , null                       // Icon (none)
                                 , choices                    // Button text as above.
                                 , "None of your business"    // Default button's label
                               );

                //... Use a switch statement to check which button was clicked.
                switch (response) {
                    case 0:
                        democraticCount++;
                        break;

                    case 1:
                        republicanCount++;
                        break;

                    case 2:
                        noAnswerCount++;
                        break;

                    case 3:
                    case -1:
                        //... Both the quit button (3) and the close box(-1) handled here.
                        System.exit(0);

                    default:
                        //... If we get here, something is wrong.  Defensive programming.
                        JOptionPane.showMessageDialog(null, "Unexpected response " + response);
                }

                //... Display the accumulated results so far.
                JOptionPane.showMessageDialog(null, "Response = " + response
                                                  + "\nDem = " + democraticCount
                                                  + "\nRep = " + republicanCount
                                                  + "\nOther = " + noAnswerCount);

            }
        }
    }

## Notes

Line 11
  This defines an array of strings that will be displayed as possible choices.
Line 24 produces the following:

  Returns an int value identifying which of the String array of options was chosen. Note that the array element numbering starts with zero, not one.

Line 27

  | Simple message dialog. The value 2 indicated that the "None of your business" choice was made by the user.

Line 30

  | Asks a yes/no/cancel question. Returns one of these values: JOptionPane.YES_OPTION JOptionPane.NO_OPTION JOptionPane.CANCEL_OPTION JOptionPane.CLOSED_OPTION Dialog box was closed. Usually treated like CANCEL_OPTION.