Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


When executed, this code produces the dialog box shown in Figure 18.6. You may notice that the Cancel button is not shown in this confirmation dialog box. This call to the showConfirmDialog method supplied two additional parameters. The first is the title for the dialog box, Confirmation Required in this example. The second additional parameter specifies which buttons to display. In this example, the YES_NO_OPTION was given, specifying that only the Yes and No buttons should be displayed.


Figure 18.6  Yes/No dialog box.

Table 18.4 lists the constants that can be given for the type parameter to the showConfirmDialog method. The type parameter determines which buttons are displayed as well as the possible values returned. Each dialog box can return the CLOSED_OPTION constant. This constant is returned if the user closes the dialog box without selecting any of the buttons in the dialog box. The CLOSED_OPTION will typically be handled in the same way as the NO_OPTION or CANCEL_OPTION.

Table 18.4 Confirmation Dialog Box Constants

Parameter Values Buttons Possible Return

YES_NO_OPTION Yes and No YES_OPTION, NO_OPTION, CLOSED_OPTION
YES_NO_CANCEL_OPTION Yes, No, Cancel YES_OPTION, NO_OPTION, CANCEL_OPTION, CLOSED_OPTION
OK_CANCEL_OPTION OK and Cancel OK_OPTION, CANCEL_OPTION, CLOSED_OPTION
DEFAULT_OPTION OK OK_OPTION, CLOSED_OPTION

An example of a dialog box constructed with the OK_CANCEL_OPTION is presented next. This code can be placed into the last JOptionTest application. The resulting dialog box is shown in Figure 18.7. In this example, an array of strings is given as the message parameter. This creates a multiline dialog box. Notice that the second String is a single character. This creates the blank line in the dialog box. If an empty string is given, the dialog box does not show the blank line.

String[] message = new String[3];
message[0] = “Selected objects will be deleted”;
// Single space to cause blank line in dialog box.
message[1] = “ “;
message[2] = “Do you want to continue?”;
int result = JOptionPane.showConfirmDialog( null, message,
              “Confirmation Required”,
JOptionPane.OK_CANCEL_OPTION );


Figure 18.7  OK/Cancel dialog box.

As with all the standard JOptionPane dialog boxes, the showConfirmDialog method has a variant that allows the icon to be specified with the message type parameter, or explicitly by passing an Icon parameter. When the following code is inserted into the JOptionTest application, the dialog box shown in Figure 18.8 is displayed. The ERROR_MESSAGE parameter is passed to the showConfirmDialog method, telling the JOptionPane to use the error Icon defined for the look-and-feel currently in use.

String[] message = new String[3];
message[0] = “Connection timed out”;
message[1] = “ “;
message[2] = “Do you wish to continue?”;
int result = JOptionPane.showConfirmDialog( null, message,
 “Line Time Out”, JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE );


Figure 18.8  Error confirmation dialog box.

Input Dialog Boxes

There are many situations when a program needs to get input from the user. The JOptionPane class provides the showInputDialog to query the user for simple one-line input requirements. A simple input dialog box can be displayed with the following single line of code. The input dialog box displayed is shown in Figure 18.9. In this example, the dialog box is displayed with the default title, icon, and buttons. The default icon is the icon used with the QUESTION_MESSAGE message type.

String reply = JOptionPane.showInputDialog( null, “Enter your name:” );


Figure 18.9  Simple input dialog box.

Similar to the showConfirmDialog method, the showInputDialog method blocks the calling thread until the user dismisses the dialog box. The showInputDialog method returns a String containing the text entered by the user. If the Cancel button is clicked or the window is closed, a null sString is returned. If the user clicks the “OK” button without entering anything in the text area of the dialog box, an empty String is returned. This allows the calling code to distinguish between the various actions the user may have performed.

One of the overloaded versions of the showInputDialog method takes a title String, and message type parameter. A typical example of a warning input dialog box can be displayed with the line of code presented in the main method of the following version of the JOptionTest application. After the dialog box is dismissed, the application echoes the user’s input to the console. The resulting dialog box is shown in Figure 18.10.

package com.foley.test;

import javax.swing.*;

/**
 * An application that displays a JOptionPane dialog box.
 *
 * @author Mike Foley
 **/
public class JOptionTest extends Object {

    /**
     * Application entry point.
     * Display a dialog box from the JOptionPane class.
     *
     * @param args Command line parameter. Not used.
     **/
    public static void main( String args[] ) {

        String reply = JOptionPane.showInputDialog( null,
                                “Enter log name:”,
                                “Database Troubles”,
                                JOptionPane.WARNING_MESSAGE );
        System.out.println( “User entered: “ + reply );

        System.exit(0);

    } // main

} // JOptionTest


Figure 18.10  Warning input dialog box.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.