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


Selection

Perhaps the most interesting variant of the showInputDialog methods takes an array of Object instances and an Object reference as its last two parameters. The array parameter defines a list of choices to be presented to the user. The single Object parameter should be one of the items in the array. This will be the initially selected item in the choice. The presentation of the choice to the user is not defined in the JFC specification. It is left for the look-and-feel implementation. However, each of the look-and-feel implementations contained in the JFC use a JComboBox for presentation.

An example of using an array of Object instances in an input dialog box is presented next. An array of String instances is created and initialized. In the call to showInputDialog, the array is passed as the selection values, and the third item in the array is specified as the initially selected item, the string “Green” in this example. After the user dismisses the dialog box, the test application echoes the user’s choice to the console. The dialog box is shown in Figure 18.11. The dialog box created in this example does not contain an icon. This is because the PLAIN_MESSAGE type was specified for the dialog box.

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[] colors = { “Red”, “Yellow”, “Green”, “Black” };
    Object selected = JOptionPane.showInputDialog( null,
                                “Enter log name:”,
                                “Database Troubles”,
                                JOptionPane.PLAIN_MESSAGE,
                                null,
                                colors,
                                colors[2] );
        System.out.println( “User selected: “ + selected );

        System.exit(0);

    } // main

} // JOptionTest


Figure 18.11  Selection input dialog box.

Notice how the return value is an Object. The returned Object is the selected item if the user clicks the “OK” button. If “Cancel” is chosen, or the window is closed, null is returned from the showInputDialog method.

Both the array of selection items and the initially selected items are Objects. This allows any type of Object to be selected in the dialog box. For most types of Objects, the toString method is called to determine what is placed in the choice for the user. However, if the array contains icons, the icon itself is placed in the choice presented to the user.

Providing Custom Options

If the button combinations provided by the predefined option choice constants don’t meet the needs of a dialog box, the showOptionDialog method is provided to customize the option buttons contained in the dialog box. This variant of the show dialog method takes an array of Object instances that are used as the options presented to the user. It also takes an Object parameter that is one of the items in the array. This Object is the default option for the dialog box. If the items in the array are Components, they are used directly. An Icon option is placed on a JButton instance and added to the dialog box. Other types of Objects have their toString method called to determine the String that is used for the text on a JButton. The instance of the JButton is then added to the dialog box.

An example of the showOptionDialog method is to create an Abort, Retry, Cancel dialog box. An array of Strings can be used for the options and passed to the showOptionDialog method. The following code creates this dialog box. The dialog box created in this example is shown in Figure 18.12.

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[] options = { “Abort”, “Retry”, “Cancel” };
        int choosen = JOptionPane.showOptionDialog( null,
                                “The device is not ready”,
                                “Device Error”,
                                JOptionPane.YES_NO_OPTION,
                                JOptionPane.ERROR_MESSAGE,
                                null,
                                options,
                                options[2] );

        //
        // Returned value depends on position of items in option
        // array parameter.  Changing this array forces this switch
        // statement to be changed as well.
        //
        switch( choosen ) {

            case 0:
            // Abort selected
            System.out.println( “Abort choosen” );
            break;

            case 1:
            // Retry selected
            System.out.println( “Retry choosen” );
            break;

            case 2:
            // Cancel selected
            System.out.println( “Cancel choosen” );
            break;

            case JOptionPane.CLOSED_OPTION:
            // User closed the box.  Nothing selected.
            System.out.println( “Dialog Closed” );
            break;

        } // switch( reply )

        System.exit(0);
    } // main

}// JOptionTest


Figure 18.12  Abort, Retry, Cancel 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.