Message Type
The message type parameter is used to define the category of the dialog box. Possible values for this parameter are ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, and PLAIN_MESSAGE. The look-and-feel currently being used can interpret this parameter to decide which icon to display and modify the dialog accordingly. There might be differing layouts and icons for each message type. The default icons for each message type for a given look-and-feel are shown in Table 18.1. Notice that the PLAIN_MESSAGE message type provides a dialog box that does not contain an icon. Examples of each of the message types will be given in the Predefined Dialog Boxes section later in this chapter.
Table 18.1 Icons for Message Types
|
Message Type
| Java LAF
| Motif LAF
| Windows LAF
|
|
ERROR_MESSAGE
|  |  |  |
INFORMATION_MESSAGE
|  |  |  |
PLAIN_MESSAGE
| None
| None
| None
|
QUESTION_MESSAGE
|  |  |  |
WARNING_MESSAGE
|  |  |  |
|
Option Type
The final common parameter to the show family of methods is the option type. This parameter defines the set of buttons that will appear in the dialog box. The JOptionPane class defines constants for many typical use cases. The available constants, and the buttons shown in the dialog box when each constant is used, are shown in Table 18.2.
Table 18.2 Option Types
|
Parameter
| Buttons
|
|
YES_NO_OPTION
| Yes and No
|
YES_NO_CANCEL_OPTION
| Yes, No, and Cancel
|
OK_CANCEL_OPTION
| OK and Cancel
|
DEFAULT_OPTION
| OK
|
|
Note: The text that appears on the buttons in the JOptionPane dialog box has been internationalized. The text is read from a resource file, and may not appear exactly as shown throughout this chapter when used with a JDK configured for a language other than English.
Properties
Instances of the JOptionPane class fire PropertyChangeEvent messages when values of bound properties change. Interested parties can add themselves as a property change listener to be notified when the JOptionPane instance has been modified.
Table 18.3 gives a list of the bound properties contained in the JOptionPane class, and the method used to alter the property.
Table 18.3 JOptionPane Property Types
|
Property
| Setter Method
| Getter Method
|
|
ICON_PROPERTY
| setIcon
| getIcon
|
INITIAL_SELECTION_ | setInitialSelectionValue
| getInitialSelectionValue
|
VALUE_PROPERTY
|
|
|
INITIAL_VALUE_PROPERTY
| setInitialValue
| getInitialValue
|
INPUT_VALUE_PROPERTY
| setInputValue
| getInputValue
|
MESSAGE_PROPERTY
| setMessage
| getMessage
|
MESSAGE_TYPE_PROPERTY
| setMessageType
| getMessageType
|
OPTION_TYPE_PROPERTY
| setOptionType
| getOptionType
|
OPTIONS_PROPERTY
| setOptions
| getOptions
|
SELECTION_VALUES_PROPERTY
| setSelectionValues
| getSelectionValues
|
VALUE_PROPERTY
| setValue
| getValue
|
WANTS_INPUT_PROPERTY
| setWantsInput
| getWantsInput
|
|
Predefined Dialog Boxes
The JOptionPane class contains static methods for displaying many predefined dialog boxes. These are the show methods that were discussed earlier in the chapter. Using these methods allows the application to present a dialog box to the user with a single line of code. These methods encompass the most generally used portions of the JOptionPane class. The following sections present each of these methods in detail.
|