Table of Contents
In JDK 1.2 and beyond, it’s possible, using Swing, to change the way things appear and behave. This is commonly known as “Look and Feel”, or LAF, or L&F. Each platform has its own specific look and feel, and many, including Microsoft Windows and Apple Macintosh, are protected by patents, etc.
JavaSoft came up with their own, platform-neutral L&F, originally called Metal, but now simply called the Java L&F, which is available on all platforms. They have also implemented the Microsoft Windows LAF, the Motif LAF, and the Macintosh LAF. However, because of legal issues, the Microsoft Windows and Macintosh LAFs are only available on their respective platforms.
By default, Java programs display with the Java L&F. You can change this by setting the swing.defaultlaf property to the fully-qualified name of the desired L&F class.
You can also specify the desired L&F at run time. One approach is to set the L&F at the beginning of your Java program. Another is to change the L&F completely dynamically.
Example
Here’s a program that allows you to dynamically switch between various LAFs:
package swingExamples;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
class LookAndFeelPanel extends JPanel
{
public LookAndFeelPanel()
{
UIManager.LookAndFeelInfo[] feels =
UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo feel: feels)
makeButton( feel.getName(), feel.getClassName() );
}
private void makeButton(String feelName,
final String feelClassName)
{
JButton button = new JButton(feelName);
add(button);
button.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
UIManager.setLookAndFeel(feelClassName);
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
catch (InstantiationException ex)
{
ex.printStackTrace();
}
catch (IllegalAccessException ex)
{
ex.printStackTrace();
}
catch (UnsupportedLookAndFeelException ex)
{
ex.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(
LookAndFeelPanel.this);
}
}
);
}
}
class LookAndFeelFrame extends JFrame
{
public LookAndFeelFrame()
{
setTitle("Setting Look and Feel");
setSize(300, 200);
LookAndFeelPanel panel = new LookAndFeelPanel();
getContentPane().add(panel);
}
}
public class LookAndFeelDisplay
{
public static void main(String[] args)
{
JFrame frame = new LookAndFeelFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Here is a what it shows me on my Windows system, for Java 1.6:




You can tell which LAF is which by looking at the button that has the current focus. (Of course, each L&F has its own way of indicating that.)
Note: In JDK 1.4.2, JavaSoft added a GTK+ L&F (GTK+ is a popular GUI interface for Linux). Unfortunately, it doesn’t seem to have made it into JDK 1.5, or 1.6, at least in the Microsoft Windows release.
Metal & Themes
The original Java Look and Feel was code-named “Metal”, and that’s pretty much what it looked like. It was very flat (two-dimensional), dull, colorless, and generally pretty boring, especially when compared with current GUI L&Fs on Windows and on various Linux systems. Here’s an example of the original “Metal” look and feel, an image from the SwingSet demo:

The marketing people decided, in their infinite wisdom, to call “Metal” the “Java Look and Feel” (what imagination!), but most people continued to refer to it as “Metal”.
Themes
Then, in Java 5.0 (a.k.a. 1.5), the Java developers added the concept of “Themes” to the “Metal” Look and Feel, and made the default theme be one called “Ocean”. The original Metal default became the “Steel” theme.
What you’ve seen so far in most of the examples, is the Ocean theme of the Java Look and Feel. Here’s what “Ocean” looks like, to contrast it with the above:

A definite improvement, although it’s still pretty two-dimensional. At least the buttons are made to look like they have a three-dimensional component.
For more details about Metal themes, see here.
Synth
The newest look and feel for the Java 5 release is called “Synth”. It allows you to customize your own look and feel by means of an XML file, instead of having to perform lots of nitty-gritty coding in lots of different places.
The Synth look and feel class is:
javax.swing.plaf.synth.SynthLookAndFeel
and you can find its API here.
How to use Synth is beyond the scope of this course, however.
Future Efforts
More definitely needs to be done, and people are working on it.
