Java Reference
In-Depth Information
FIGURE 10.5
A confirm dialog
box.
Another method offers more options for the dialog box:
showConfirmDialog(
Component
,
Object
,
String
,
int
,
int
)
. The first two arguments are the same as those in other
showConfirmDialog()
methods. The last three arguments are the following:
A string that will be displayed in the dialog box's title bar.
n
An integer that indicates which option buttons will be shown; it should be equal to
one of the class constants:
YES_NO_CANCEL_OPTION
or
YES_NO_OPTION
.
n
An integer that describes the kind of dialog box it is, using the class constants
ERROR_MESSAGE
,
INFORMATION_MESSAGE
,
PLAIN_MESSAGE
,
QUESTION_MESSAGE
, or
WARNING_MESSAGE
. (This argument is used to determine which icon to draw in the
dialog box along with the message.)
n
10
For example:
int response = JOptionPane.showConfirmDialog(null,
“Error reading file. Want to try again?”,
“File Input Error”,
JOptionPane.YES_NO_OPTION,
JOptionPane.ERROR_MESSAGE);
Figure 10.6 shows the resulting dialog box.
FIGURE 10.6
A confirm dialog
box with Yes and
No buttons.
Input Dialog Boxes
An input dialog box asks a question and uses a text field to store the response. Figure
10.7 shows an example.
The easiest way to create an input dialog box is with a call to the
showInputDialog(
Component
,
Object
)
method. The arguments are the parent compo-
nent and the string, component, or icon to display in the box.