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 apps 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 Screenshot-13.

Screenshot-13. Using a confirmation dialog
Java ScreenShot

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 app while they are showing. Here's a slightly fancier message dialog. We've specified a title for the dialog and a message type, which affects the icon that is displayed:

 JOptionPane.showMessageDialog(frame, "You are low on memory.",
 "Apocalyptic message", JOptionPane.WARNING_MESSAGE);


Here's how to display the confirmation dialog shown in Screenshot-13:

 int result = JOptionPane.showConfirmDialog(null,
 "Do you want to remove Windows now?");


In this case, we've passed null for the parent component and it will be displayed centered on the screen. Special values are returned from showConfirmDialog( ) to indicate which button was pressed. A full example later in this section shows how to use this return value. Sometimes you need to ask the user to type some input. The following code puts up a dialog requesting the user's name:

 String name = JOptionPane.showInputDialog(null,
 "Please enter your name.");


Whatever the user types is returned as a String or null if the user presses the Cancel button. The most general type of dialog is the option dialog. You supply an array of objects you wish to be displayed; JOptionPane takes care of formatting them and displaying the dialog. The following example displays a text label, a JTextField, and a JPasswordField. (Text components are described in the next chapter.)

 JTextField userField = new JTextField( );
 JPasswordField passField = new JPasswordField( );
 String message = "Please enter your user name and password.";
 result = JOptionPane.showOptionDialog(frame,
 new Object[] { message, userField, passField },
 "Login", JOptionPane.OK_CANCEL_OPTION,
 JOptionPane.QUESTION_MESSAGE,
 null, null, null);


We've also specified a dialog title ("Login") in the call to showOptionDialog( ). We want OK and Cancel buttons, so we pass OK_CANCEL_OPTION as the dialog type. The QUESTION_MESSAGE argument indicates we'd like to see the question-mark icon. The last three items are optional: an Icon, an array of different choices, and a current selection. Since the icon parameter is null, a default is used. If the array of choices and the current selection parameters were not null, JOptionPane might try to display the choices in a list or combo box. The following app includes all the examples we've covered:

 import javax.swing.*;
 public class ExerciseOptions {
 public static void main(String[] args) {
 JFrame frame = new JFrame("ExerciseOptions v1.0");
 frame.setSize(200, 200);
 frame.setVisible(true);
 JOptionPane.showMessageDialog(frame, "You have mail.");
 JOptionPane.showMessageDialog(frame, "You are low on memory.",
 "Apocalyptic message", JOptionPane.WARNING_MESSAGE);
 int result = JOptionPane.showConfirmDialog(null,
 "Do you want to remove Windows now?");
 switch (result) {
 case JOptionPane.YES_OPTION:
 System.out.println("Yes"); break;
 case JOptionPane.NO_OPTION:
 System.out.println("No"); break;
 case JOptionPane.CANCEL_OPTION:
 System.out.println("Cancel"); break;
 case JOptionPane.CLOSED_OPTION:
 System.out.println("Closed"); break;
 }
 String name = JOptionPane.showInputDialog(null,
 "Please enter your name.");
 System.out.println(name);
 JTextField userField = new JTextField( );
 JPasswordField passField = new JPasswordField( );
 String message = "Please enter your user name and password.";
 result = JOptionPane.showOptionDialog(frame,
 new Object[] { message, userField, passField },
 "Login", JOptionPane.OK_CANCEL_OPTION,
 JOptionPane.QUESTION_MESSAGE,
 null, null, null);
 if (result == JOptionPane.OK_OPTION)
 System.out.println(userField.getText( ) +
 " " + new String(passField.getPassword( )));
 System.exit(0);
 }
 }


Java ScreenShot
Comments