The Java TM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Creating a GUI with JFC/Swing
Lesson: Using Other Swing Features

## How to Set the Look and Feel

> If you don't care which look and feel your program uses, you can skip this section entirely. For example, most of the programs in this trail don't specify the look and feel, so that you can easily run the programs with your preferred look and feel.

> When a program does not set its look and feel, the Swing UI manager must figure out which look and feel to use. It first checks whether the user has specified a preferred look and feel. If so, it attempts to use that. If not, or if the user's choice isn't valid, then the UI manager chooses the Java Look & Feel.

> - Setting the Look and Feel
> - How the UI Manager Chooses the Look and Feel
> - Changing the Look and Feel After Startup

### Setting the Look and Feel

> To programmatically specify a look and feel, use the `UIManager.setLookAndFeel` method. For example, the bold code in the following snippet makes the program use the Java Look & Feel:

> >     public static void main(String[] args) {
> >         try {
> >             UIManager.setLookAndFeel(
> >                 UIManager.getCrossPlatformLookAndFeelClassName());
> >         } catch (Exception e) { }

> >         new SwingApplication(); //Create and show the GUI.
> >     }

> The argument to `setLookAndFeel` is the fully qualified name of the appropriate subclass of `LookAndFeel`. To specify the Java Look & Feel, we used the `getCrossPlatformLookAndFeelClassName` method. If you want to specify the native look and feel for whatever platform the user runs the program on, use `getSystemLookAndFeelClassName`, instead. To specify a particular UI, you can use the actual class name. For example, if you design a program to look best with the Windows Look & Feel, you can use this code to set the look and feel:

> >     UIManager.setLookAndFeel(
> >     	    "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

> Here are some of the arguments you can use for `setLookAndFeel`:

> `UIManager.getCrossPlatformLookAndFeelClassName()`
>   Returns the string for the one look-and-feel guaranteed to work -- the Java Look & Feel.
> `UIManager.getSystemLookAndFeelClassName()`
>   Specifies the look and feel for the current platform. On Win32 platforms, this specifies the Windows Look & Feel. On Mac OS platforms, this specifies the Mac OS Look & Feel. On Sun platforms, it specifies the CDE/Motif Look & Feel.
> `"javax.swing.plaf.metal.MetalLookAndFeel"`
>   Specifies the Java Look & Feel. (The codename for this look and feel was Metal.) This string is the value returned by the `getCrossPlatformLookAndFeelClassName` method.
> `"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"`
>   Specifies the Windows Look & Feel. Currently, you can use this look and feel only on Win32 systems.
> `"com.sun.java.swing.plaf.motif.MotifLookAndFeel"`
>   Specifies the CDE/Motif Look & Feel. This look and feel can be used on any platform.
> `"javax.swing.plaf.mac.MacLookAndFeel"`
>   Specifies the Mac OS Look & Feel, which can be used only on Mac OS platforms.

> You aren't limited to the preceding arguments. You can specify the name for any look and feel that is in your program's class path.

### How the UI Manager Chooses the Look and Feel

> Here are the look-and-feel determination steps that occur when the UI manager first initializes itself:
> 1. If the program sets the look and feel before any components are created, the UI manager tries to create an instance of the specified look-and-feel class. If successful, all components use that look and feel.

> 2. If the program hasn't successfully specified a look and feel, then before the first component's UI is created, the UI manager tests whether the user specified a look and feel in a file named `swing.properties`. It looks for the file in the `lib` directory of the Java release. For example, if you're using the Java interpreter in `javaHomeDirectory\bin`, then the `swing.properties` file (if it exists) is in `javaHomeDirectory\lib`. If the user specified a look and feel, then again the UI manager tries to instantiate the specified class. Here is an example of the contents of a `swing.properties` file:

>    >     # Swing properties

>    >     swing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel

> 3. If neither the program nor the user successfully specifies a look and feel, then the program uses the Java Look & Feel.

### Changing the Look and Feel After Startup

> You can change the look and feel with `setLookAndFeel` even after the program's GUI is visible. To make existing components reflect the new look and feel, invoke the `SwingUtilities` `updateComponentTreeUI` method once per top-level container. Then you might wish to resize each top-level container to reflect the new sizes of its contained components. For example:

> >     UIManager.setLookAndFeel(lnfName);
> >     SwingUtilities.updateComponentTreeUI(frame);
> >     frame.pack();

| Start of Tutorial > Start of Trail > Start of Lesson | Search