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


Actions to Set the Look-and-Feel

Swing components are shown in the various look-and-feel implementations provided with the JFC throughout this book. The following action is used to set the look-and-feel for an application. When invoked, the SetLookAndFeelAction will update the look-and-feel to that named by the lookAndFeelName parameter specified for the action. It will also update the UI of components registered in the framesToUpdate vector and the component that is the source of the ActionEvent that invoked that action. The methods that actually change the look-and-feel and update the component tree are presented in Chapter 30, “Pluggable Look-and-Feel,” and Chapter 33, “Accessibility.” There are also extensions of the SetLookAndFeelAction that are initialized for the standard look-and-feel implementations that are shipped with the JFC. The name of one of these actions embeds the name of the look-and-feel that it will activate. For example, the action to set the MS Windows look-and-feel is named SetWindowsLookAndFeelAction. Each of these actions is part of the com.foley.utility package shown in Appendix C, “com.foley.utility Source Code.”

package com.foley.utility;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

/**
 * Class SetLookAndFeelAction
 * <p>
 * An action which sets the look and feel to the given look
 * and feel class name.
 * <p>
 * @author Mike Foley
 * @version 1.2
 **/
public class SetLookAndFeelAction extends AbstractAction {

    /**
     * The default name used in a menu.
     **/
    private static final String DEFAULT_NAME = “Set Look and Feel”;

    /**
     * The key into our property table of the name of the
     * class containing the look and feel.
     **/
    private static final String LOOKANDFEELNAME_KEY
                           = “LookAndFeelNameKey”;

    /**
     * The key into our property table for the collection of
     * Frames to update when this action is invoked.
     **/
    private static final String FRAMESTOUPDAT_KEY
                           = “FramesToUpdateKey”;

    /**
     * Initialize the Action with the given name and L&F name.
     * <p>
     * @param name The name for the Action.
     * @param lookAndFeelName The look and feel class name.
     **/
    public SetLookAndFeelAction( String name, String lookAndFeelName ) {
        this( name, null, lookAndFeelName, null );
    } // SetLookAndFeelAction

    /**
     * Initialize the Action with the given name and L&F name.
     * <p>
     * @param name The name for the Action.
     * @param icon The Icon for the Action.
     * @param lookAndFeelName The look and feel class name.
     * @param framesToUpdate Each Component in this array will have its
     *                       component tree udpated when this action is
     *                       invoked.
     **/
    public SetLookAndFeelAction( String name, Icon icon,
                                 String lookAndFeelName,
                                 Vector framesToUpdate ) {
        super( name );

        if( lookAndFeelName == null )
            throw new RuntimeException(
                 “Look-and-feel name may not be null.” );

        if( icon != null )
            putValue( SMALL_ICON, icon );
        putValue( LOOKANDFEELNAME_KEY, lookAndFeelName );
        if( framesToUpdate != null )
            putValue( FRAMESTOUPDAT_KEY, framesToUpdate );

    } // SetLookAndFeelAction

    /**
     * actionPerformed, from ActionListener
     * <p>
     * Perform the action.  Set the look and feel
     * to that defined by the look-and-feel attribute.
     * Update the source frame, and frames in the
     * frames Vector.
     * <p>
     * @param event The event causing the action to fire.
     **/
    public void actionPerformed( ActionEvent event ) {

        try {
            String lookAndFeelName = ( String )getValue(
                                         LOOKANDFEELNAME_KEY );
            UIManager.setLookAndFeel( lookAndFeelName );

            //
            // Update component tree for the source of the event.
            //
            Object o = event.getSource();
            if( o instanceof Component ) {
                Frame frame = JOptionPane.getFrameForComponent(
                                    ( Component )o );
                SwingUtilities.updateComponentTreeUI( frame );
            }

            //
            // See if there are any registered frames to update.
            //
            Vector framesToUpdate = ( Vector )getValue(
                                       FRAMESTOUPDAT_KEY );
            if( framesToUpdate != null ) {
                for( Enumeration e = framesToUpdate.elements();
                              e.hasMoreElements(); ) {
                    Object f = e.nextElement();
                    if( f instanceof Component ) {
                        Frame frame = JOptionPane.getFrameForComponent(
                                            ( Component )f );
                        SwingUtilities.updateComponentTreeUI( frame );
                    }
                }
            }
        } catch( ClassNotFoundException cnf ) {
            throw new RuntimeException( cnf.getLocalizedMessage() );
        } catch( InstantiationException ie ) {
            throw new RuntimeException( ie.getLocalizedMessage() );
        } catch( UnsupportedLookAndFeelException ulf ) {
            throw new RuntimeException( ulf.getLocalizedMessage() );
        } catch( IllegalAccessException ia ) {
            throw new RuntimeException( ia.getLocalizedMessage() );
        } // catch
    } // actionPerformed

} // SetLookAndFeelAction


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.