Java Reference
In-Depth Information
Pluggable Look and Feel
Swing components have what is referred to as a pluggable look and feel (PLAF), allowing
their appearance to be independent of the underlying platform. You can create your own
look and feel—determining colors, fonts, and backgrounds for all your components. Creat-
ing your own look and feel involves a fair amount of coding and artistic skills and is beyond
the scope of this topic.
However, Swing comes with a built-in look and feel for Windows, Macintosh, and Motif,
which is a Unix look and feel. There is also a default Swing look and feel, known as the
Metal look and feel. (In its earliest stages, Swing was referred to as Metal.)
The javax.swing.UIManager class maintains the current look and feel for a Java program
using Swing. In particular, the UIManager class contains methods for determining and
changing the current look and feel:
public static void setLookAndFeel(LookAndFeel x). Throws Unsupported-
LookAndFeelException. Changes the current look and feel for this Java program
to the given LookAndFeel object.
public static void setLookAndFeel(String s). Throws UnsupportedLookAnd-
FeelException. Changes the current look and feel for this Java program to the given
class name, which is a class that implements LookAndFeel.
public static LookAndFeel getLookAndFeel().
Returns the current look and
feel for this Java program as a LookAndFeel object.
The LookAndFeel class is in the javax.swing package and encapsulates a pluggable look
and feel. The following classes represent the four built-in pluggable look-and-feel compo-
nents of Swing:
com.sun.java.swing.plaf.motif.MotifLookAndFeel. Creates a Motif look and
feel, which is the user interface for many Unix operating systems.
com.sun.java.swing.plaf.windows.WindowsLookAndFeel.
Creates a
Microsoft Windows look and feel.
com.sun.java.swing.plaf.mac.MacLookAndFeel. Creates a Macintosh look and
feel. (Note that as of J2SE 1.4, this look and feel is not completed yet.)
javax.swing.plaf.metal.MetalLookAndFeel.
Creates a Metal look and feel,
which is the default for Swing applications.
The PLAFDemo program available on the Web site demonstrates a Swing JFrame with
various Swing components added to the JFrame. The JFrame class represents a typical GUI
window, and is one of the few heavyweight Swing components. You will notice in the out-
puts of the PLAFDemo program that the window containing the components is not
affected by the selected look and feel.
Figure 12.5 shows the output of the PLAFDemo program using the Metal, Motif, and
Windows look and feel. Compare the three windows and notice that the components are
similar, but each has its own unique “look.” The “feel” comes from the actual response of
the components when the user interacts with them.
Search WWH ::




Custom Search