1. Computing

Creating a Message Dialog Box - Part II

By

In Creating a Message Dialog Box - Part I the showMessageDialog method of the JOptionPane class was used to make simple message dialog boxes. Those boxes had an OK button that allowed the user to dismiss the window. If you want to get some feedback from a message box there are two other methods that can be used - showConfirmDialog and showOptionDialog.

The JOptionPane Option Fields

Similar to the message type static fields shown previously, there are option type static fields for controlling the buttons that appear on the dialog:

  • DEFAULT_OPTION
  • YES_NO_OPTION
  • YES_NO_CANCEL_OPTION
  • OK_CANCEL_OPTION

For example, if I pass JOptionPane.YES_NO_OPTION as the option type in the showConfirmDialog method, the dialog box created will have a YES button and a NO button.

The showConfirmDialog Method

The showConfirmDialog method will produce a dialog box that asks the user to confirm a message. As it can have more than just an OK button it means we can capture feedback from the user based on what button they click:

//Use the showConfirmDialog method for a warning message dialog box
//with OK, CANCEL buttons. Capture the button number with an int variable
int choice = JOptionPane.showConfirmDialog(this 
                ,"This is the dialog message" 
                ,"This is the dialog title", JOptionPane.WARNING_MESSAGE
                , JOptionPane.OK_CANCEL_OPTION);

This dialog box is similar to one created by the showMessageDialog method except for the buttons. As you can see from the option type, JOptionPane.OK_CANCEL_OPTION, this dialog box will have an OK button and a CANCEL button.

The choice variable captures the button number: CANCEL = 2, NO = 1, YES/OK = 0.

The showOptionDialog Method

The showOptionDialog method gives the most options for customizing a dialog box. You can set your own icon, and modify the buttons so that they have the text that you want:

//Using a standard Java icon
private Icon optionIcon = UIManager.getIcon("FileView.computerIcon");

//String array to be used for the buttons
String[] buttonOptions = new String[] {"Happy Button", "Sad Button"
                                            ,"Confused Button"};

//If the options parameter is not null the YES, NO, CANCEL buttons are not used
//The buttons are made with the object array - in this case a String array.  
int choice = OptionPane.showOptionDialog(this, "This is the dialog message" 
                , "This is the dialog title", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE
                   ,optionIcon, buttonOptions, buttonOptions[0]);

In the above example I'm using a standard Java icon for the dialog box. I'm also using a String array for the button text. The dialog box created will have the same number of buttons as the number of Strings in the array (i.e., 3) and each button will take on the text of one of the Strings.

Which JOptionPane Method to Use?

There are a few options to choose from when creating a simple message dialog box. The easiest way to pick which one to use is to think about how you want the user to react. Use showMessageDialog when you want to inform the user of something which they can easily dismiss. If you need the user to confirm a question with a YES or OK then use showConfirmDialog. If you want to customize the choice options then use showOptionDialog.

The full Java code listing for simple message box examples can be Simple Message Dialog Boxes Program. Example code of an application that goes through most of the options for showMessageDialog, showConfirmDialog, showOptionDialog can be found in JOptionPane Option Chooser Program.

 

  1. About.com
  2. Computing
  3. Java
  4. Create User Interfaces
  5. Using Dialog Boxes
  6. Java Syntax: Creating a Message Dialog Box - Part II

©2014 About.com. All rights reserved.