Dialogs
A dialog is another standard feature of user interfaces.
Dialogs are frequently used to present information to the user (“Your
fruit salad is ready.”) or to ask a question (“Shall I bring the car
around?”). Dialogs are used so commonly in GUI applications that Swing
includes a handy set of prebuilt dialogs. These are accessible from static
methods in the JOptionPane class. Many
variations are possible; JOptionPane
groups them into four basic types:
- Message dialog
Displays a message to the user, usually accompanied by an OK button.
- Confirmation dialog
Ask a question and displays answer buttons—usually Yes, No, and Cancel.
- Input dialog
Asks the user to type in a string.
- Option dialogs
The most general type. You pass it your own components, which are displayed in the dialog.
A confirmation dialog is shown in Figure 17-13.

Figure 17-13. Using a confirmation dialog
Let’s look at examples of each kind of dialog. The following code produces a message dialog:
JOptionPane.showMessageDialog(frame,"You have mail.");
The first parameter to showMessageDialog() is
the parent component (in this case, frame, an existing JFrame). The dialog will be centered on the
parent component. If you pass null for
the parent component, the dialog is centered in your screen. The dialogs
that JOptionPane displays are
modal, which means they block other input to your
application while they are showing.
Here’s a slightly ...