Application Techniques



Specifying a Pluggable Look and Feel

How to specify a pluggable look and feel (PLAF) for a form that uses Swing-based controls.

About this technique

Details

Category

Java Client Techniques> Version 3 Swing-based controls

Description

You'll learn about:

Related reading

See the chapter on advanced form techniques in the Programmer's Guide

JDK 1.2 (Java 2) provides one or more look and feel settings for Swing controls. The available settings depend on your user's operating system. You can change the look and feel for your form programmatically or provide controls that let the user choose a look and feel.

Getting the look and feel settings for the form   Top of page

To get or set the look and feel, use static methods of the UIManager class.

To find out what settings are installed on a user's system, call getInstalledLookAndFeels(). It returns an array of LookAndFeelInfo objects.

  public UIManager.LookAndFeelInfo[] gUIinfo; 
     gUIinfo = UIManager.getInstalledLookAndFeels(); 

This code selects an installed look and feel from the array.

  UIManager.setLookAndFeel( gUIinfo[0].getClassName() ); 

This code displays the current look and feel in a label control.

  lblLookAndFeel.setText( "Current L&F: " + 
     UIManager.getLookAndFeel().getID() ); 

Notes on the code

Changing the look and feel of the form   Top of page

To set a specific look and feel, you can specify the class name. "Metal" is the old name for the Java cross-platform look and feel.

  UIManager.setLookAndFeel( 
     "javax.swing.plaf.metal.MetalLookAndFeel" ); 

This code specifies the cross-platform look and feel by calling a method to get the current cross-platform class name.

  UIManager.setLookAndFeel( 
     UIManager.getCrossPlatformLookAndFeelClassName() ); 

If you set the look and feel in the formLoaded event, the form is drawn with the new settings. However, if the form is already displayed, you must update the display when you make the change. This code changes the form to the system look and feel and tells the form to repaint.

  UIManager.setLookAndFeel( 
     UIManager.getSystemLookAndFeelClassName()); 
  SwingUtilities.updateComponentTreeUI(getFrame()); 

It is better to specify the look and feel before the form is displayed. Changing the look and feel when the form is already displayed can cause some controls to paint in correctly.

Notes on the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.