|
Changing the Look and Feel (LAF)
|
|---|
1. Default is "Java LAF" (or "Metal"), a custom look and feel similar to
the Windows look.
2. Motif look available on all platforms. Windows and Mac looks
are only available on their native platforms.
- It is technically easy to work around this restriction, but it
is not currently legal to distribute/use applications that do this.
3. You can change look at runtime.
- Sounds cool, but rarely used in real life.
- Normal place to set look is in constructor of top-level
JFrame (or main), or in init
in a JApplet.
4. You can get AWT behavior of using native look.
- Call the
getSystemLookAndFeelClassName method of
UIManager, and pass result to
UIManager.setLookAndFeel.
- Since
setLookAndFeel throws an exception, this is a
bit inconvenient, and you might want to make a static method in a
utility class called "setNativeLookAndFeel".
See example below in
WindowUtilities.java.
5. Changing the Look and Feel: Examples
Here are some utility functions that set the LAF to native, Java
(Metal), and Motif, respectively. Since I think that most users will
expect the native LAF, I almost always call
setNativeLookAndFeel at the beginning of Swing programs.
WindowUtilities.java
(Download source code)
import javax.swing.*;
import java.awt.*;
public class WindowUtilities {
public static void setNativeLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Error setting native LAF: " + e);
}
}
public static void setJavaLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {
System.out.println("Error setting Java LAF: " + e);
}
}
public static void setMotifLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch(Exception e) {
System.out.println("Error setting Motif LAF: " + e);
}
}
...
}
6. Example: Windows LAF (Sun SwingSet Demo)
7. Example: Motif LAF (Sun SwingSet Demo)
8. Example: Java (Metal) LAF (Sun SwingSet Demo)
This page is part of my
Quick Swing Tutorial for AWT Programmers. © 1999
Marty Hall. All
source code freely available for unrestricted use. Created for for
work in the Research
and Technology Development Center of the Johns Hopkins University
Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS
Program in Computer Science, and for various industry seminars and
Java short courses.