Java Reference
In-Depth Information
Listing 20-1. Listing Looking and Feel Classes
import javax.swing.*;
public class ListPlafs {
public static void main (String args[]) {
UIManager.LookAndFeelInfo plaf[] = UIManager.getInstalledLookAndFeels();
for (int i=0, n=plaf.length; i<n; i++) {
System.out.println("Name: " + plaf[i].getName());
System.out.println(" Class name: " + plaf[i].getClassName());
}
}
}
Running the program might generate the following output. Your current system configu-
ration and/or changes to future versions of the Swing libraries could alter this result somewhat.
Name: Metal
Class name: javax.swing.plaf.metal.MetalLookAndFeel
Name: CDE/Motif
Class name: com.sun.java.swing.plaf.motif.MotifLookAndFeel
Name: Windows
Class name: com.sun.java.swing.plaf.windows.WindowsLookAndFeel
Note Ocean is not a look and feel in and of itself. Instead, it is a built-in theme of the Metal look and feel.
This theme happens to be the default for Metal.
Changing the Current Look and Feel
Once you know which look and feel classes are available on your system, you can have your
programs use any one of them. The UIManager has two overloaded setLookAndFeel() methods
for changing the installed look and feel class:
public static void setLookAndFeel(LookAndFeel newValue) throws
UnsupportedLookAndFeelException
public static void setLookAndFeel(String className) throws
ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException
Although the first version might seem to be the more logical choice, the second one is
the more frequently used version. When you ask for the installed look and feel classes with
UIManager.getInstalledLookAndFeels() , you get back the class names as strings of the objects,
not instances. Because of the exceptions that can occur when changing the look and feel, you
need to place the setLookAndFeel() call within a try / catch block. If you're changing the look
and feel for an existing window, you need to tell the component to update its appearance with
 
Search WWH ::




Custom Search