Java Reference
In-Depth Information
With the help of this new OptionPaneUtils.getSelection(JOptionPane) helper method,
you can now find out the option pane selection with one line of code, and then act accordingly
based on the response.
int selection = OptionPaneUtils.getSelection(optionPane);
switch (selection) {
case ...: ...
break;
case ...: ...
break;
default: ...
}
If you create a JOptionPane with a null options array, you can use the constants within the
JOptionPane class to indicate the position of the default button labels and their return values
from the OptionPaneUtils.getSelection(JOptionPane) method. These constants are listed in
Table 9-1. Using these constants enables you to avoid hard-coding constants such as 0, 1, 2, or -1.
Table 9-1. JOptionPane Option Position Constants
Position
Description
CANCEL_OPTION
Used when the Cancel button is pressed
CLOSED_OPTION
Used when the pop-up window closed without the user pressing a button
NO_OPTION
Used when the No button is pressed
OK_OPTION
Used when the OK button is pressed
YES_OPTION
Used when the Yes button is pressed
Automatically Creating a JOptionPane in a Pop-Up Window
You can manually create a JOptionPane , place it in a JDialog or JInternalFrame (or any other
container), and fetch the response. Alternatively, you could use the JOptionPane factory methods
for creating JOptionPane components directly within either a JDialog or a JInternalFrame . Using
the many factory methods, you can create the option pane, place it in a pop-up window, and
get the response with a single line of source code.
There are 25 methods, which are first broken down into two sets: those that create the
JOptionPane and show it within a JDialog and those that show the pane within a JInternalFrame .
Methods that show the JOptionPane within a JInternalFrame are named showInternal XXX Dialog() ,
and methods that create the pane within a JDialog are named show XXX Dialog() .
The second grouping of factory methods for JOptionPane is what fills in the XXX part of the
method names. This represents the various message types of option panes that you can create
and display. In addition, the message type defines what is returned after the user selects some-
thing in the option pane. The four different message types are as follows:
Search WWH ::




Custom Search