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.

3. You can change look at runtime.

4. You can get AWT behavior of using native look.

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)

SwingSet Demo (Windows LAF)

7. Example: Motif LAF (Sun SwingSet Demo)

SwingSet Demo (Motif LAF)

8. Example: Java (Metal) LAF (Sun SwingSet Demo)

SwingSet Demo (Windows LAF)


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.