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


The overridden setOptions method creates a new array of options that it will pass to the setOptions method contained in the JOptionPane class. Each instance in the given array is checked to see if it is an Action instance. When an Action is detected, a JButton is created with the name of the Action. The Action is added as an ActionListener to the new button to execute the Action when the button is clicked. The ActionListener created in the constructor is also added to each button. This will call the setValue method of the dialog box when the button is pressed. The JButton replaces the Action in the options array that will be passed to the parent’s setOptions method. If the Object in the options array is not an Action, it is not altered. When the processed array is passed to the setOptions method in JOptionPane, buttons will be added wherever Action instances were in the original options array.

Listing 18.1 ActionOptionPane Class

package com.foley.dialogs;

import java.awt.event.*;

import javax.swing.*;

/**
 * An Action aware JOptionPane extension.
 *
 * @author Mike Foley
 **/
public class ActionOptionPane extends JOptionPane {

    private final ActionListener setValueActionListener;

    /**
     * ActionOptionPane, constructor.
     *
     * Let the parent JOptionPane create and configure
     * the dialog. Set the options to the configured dialog
     * box using our version of the setOptions method.
     *
     * @param message The message for the dialog box.
     * @param messageType The type of the message.
     * @param optionType The type for the options.
     * @param icon The icon to use in the dialog box.
     * @param options The options to use for the dialog box.
* @param initialValue The initally selected option.
     *                     Must be one of the options above.
     * @see JOptionPane
     **/    public ActionOptionPane( Object message,
                    int messageType,
                    int optionType,
                    Icon icon,
                    Object options[],
                    Object initialValue) {

        super( message, messageType, optionType, icon, null, null );

        setValueActionListener = new ActionListener() {
            public void actionPerformed( ActionEvent event ) {
                setValue( event.getSource() );
            } // actionPerformed
        };

        setOptions( options );

        setInitialValue( initialValue );
    }

    /**
     * setOptions, overrided from JOptionPane
     *
     * Scan the list of options, and for each Action create
     * a JButton and add the Action as an ActionListener.
     * Then let the super class, JOptionPane, process the
     * new list of options.
     *
* @param options The options, that become buttons,
                      for the dialog box.
     **/
    public void setOptions( Object[] options) {
        Object[] ourOptions = new Object[ options.length ];
        for( int i = 0; i < options.length; i++ ) {
            if( options[ i ] instanceof Action ) {
                Action action = ( Action )options[ i ];
                JButton button = new JButton(
                              ( String )action.getValue( Action.NAME ) );
                button.addActionListener( setValueActionListener );
                button.addActionListener( action );
                ourOptions[ i ] = button;
            } else {
                ourOptions[ i ] = options[ i ];
            }
        }
        super.setOptions( ourOptions );
    }}
 // ActionOptionPane

A simple usage of the ActionOptionPane dialog class is shown in Listing 18.2. In this example, an exit confirmation dialog box is created and tied to the exit menu item. When the Exit menu item is selected, the resulting dialog box is shown in Figure 18.13. Notice that the returned value of the ActionOptionPane dialog box does not have to be checked. This is because the Action performs the function when the button is clicked. However, if the button that was clicked is required, it can be queried with the getValue method on the created ActionOptionPane.


Figure 18.13  Action-aware dialog box.

If your application is designed with a rich collection of actions, this is a very convenient method to manage dialog boxes. It also provides an easy visual presentation that allows users to invoke operations.


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.