Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


The NoOpAction class shows that writing an Action is not any more difficult that writing an ActionListener. However, an Action provides a great deal more functionality than a simple ActionListener. To demonstrate, let’s look at a more useful Action. The ExitAction is shown in the following code. When invoked, this Action shuts down the Java Virtual Machine. The class contains a static initializer to load the default Icon associated with the Action. The class contains many constructors to allow the Action to be customized at construction time. The setValue method can be used to customize an existing instance of the ExitAction class. The name given in the constructor is the name used for the Action in a menu. The Icon specified in the constructor would be used if the Action is added to a toolbar. The actionPerformed method is called to invoke the Action, in this case, exiting the application. In Chapter 9, “Menus and Toolbars,” as well as in examples throughout this book, it will be seen how to add this Action to a menu and toolbar.

package com.foley.utility;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Icon;

/**
 * Class ExitAction
 * <p>
 * An action which shuts down the Java virtual machine.
 * <p>
 * @author Mike Foley
 * @version 1.2
 **/
public class ExitAction extends AbstractAction {

    /**
     * The default name and Icon for the Action.
     **/
    private static final String DEFAULT_NAME = “Exit”;
    private static final String DEFAULT_ICON_NAME = “exit.gif”;
    private static Icon DEFAULT_ICON;

    /**
     * ExitAction, default constructor.
     * Create the Action with the default name and Icon.
     **/
    public ExitAction() {
        this( DEFAULT_NAME, DEFAULT_ICON );
    } // ExitAction

    /**
     * ExitAction, constructor.
     * Create the Action with the given name and
     * default Icon.
     *
     * @param name The name for the Action.
     **/
    public ExitAction( String name ) {
        this( name, DEFAULT_ICON );
    } // ExitAction

    /**
     * ExitAction, constructor.
     * Create the Action with the given name and
     * Icon.
     *
     * @param name The name for the Action.
     * @param icon The small icon for the Action.
     **/
    public ExitAction( String name, Icon icon ) {
            super( name, icon );
    } // ExitAction

    /**
     * putValue, overridden from AbstractAction.
     * Guard against null values being added to the Action.
     *
     * @param key The key for the value.
     * @param value The value to be added to the Action.
     **/
    public void putValue( String key, Object value ) {
        if( value != null )
            super.putValue( key, value );
    }

    /**
     * actionPerformed, from ActionListener
     * <p>
     * Perform the action.  Exit the JVM.
     * <p>
     * @param event The event causing the action to fire.
     **/
    public void actionPerformed( ActionEvent event ) {
        System.exit(0);
    } // actionPerformed

    /**
     * static initialization. Load the default Icon.
     **/
    static {
        try {
            DEFAULT_ICON = ImageLoader.loadIcon( DEFAULT_ICON_NAME );
        } catch( InterruptedException ie ) {
            System.err.println( “Could not load ExitAction default Icon” );
        }
    }
} // ExitAction

Using Action classes to encapsulate the functionality of a control, or the application itself, makes it easier to create user interfaces. This programming technique also allows the user interface and the program’s functionality to be kept separate. This makes it easier to modify the user interface without altering the application’s functional classes.


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.