Prev: JOptionPane - Simple Dialogs | Next: none

Java: JOptionPane.showOptionDialog

Example of JOptionPane.showOptionDialog

  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 
// 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);     // It would be better to exit loop, but...
                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 20 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 32: A switch statement
A switch statement is a good way to test the responses if there are very many possibilities.
Line 45: System.exit(0) is not the best way to stop this program
System.exit(0) stops the execution of a program, but it it isn't always the best choice. A better way would be to exit the loop so that any final processing could be done. This requires either an extra boolean variable to tell whether we should continue in the loop, or a break that terminates the loop. Unfortunately, the break statement is used both in the switch statement and in loops, so we have to use a labelled break, which hasn't been explained yet.
Line 52

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

It also shows a summary of the choices that have been made so far.