How to Set the Look and Feel

 < Day Day Up > 

If you don't care which look and feel your program uses, you can skip this section entirely. Most of the programs in this book don't specify the look and feel, so you can easily run them with any one you prefer.

When a program doesn't set its look and feel, the Swing UI manager must figure out which one to use. It first checks whether the user has specified a preference. If so, it attempts to use that; if not, or if the user 's choice isn't valid, the UI manager chooses the Java look and feel.

Version Note: Release 1.4.2 introduced two new look and feels: Microsoft Windows XP and GTK+.


You aren't limited to the look and feels supplied with the Java platform. You can use any of the ones in your program's class path. External look and feels are usually provided in one or more JAR files that you add to the class path at runtime. For example:

 java -classpath .;C:\java\lnfdir\newlnf.jar SwingApplication 

Once an external look and feel is in the class path, your program can use it just like any of those shipped with the Java platform.

Programmatically 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 tells the program to use the Java look and feel:

 try {  UIManager.setLookAndFeel(   UIManager.getCrossPlatformLookAndFeelClassName());  } catch (Exception e) { }  //Create and show the GUI...  

Note: If you're going to set the look and feel, you should do it as the very first step in your application. Otherwise, you run the risk of initializing the Java look and feel regardless of what one you've requested . This can happen inadvertently when a static field references a Swing class, which causes the look and feel to be loaded. If no look and feel has yet been specified, the default Java look and feel is loaded.


The argument to setLookAndFeel is the fully qualified name of the appropriate subclass of LookAndFeel . To specify the Java look and feel, use 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, use the actual class name. For example, if you design a program to look best with the GTK+ look and feel, you can use this code:

 UIManager.setLookAndFeel(             "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); 

Note: The GTK+ look and feel was released in all versions of the 1.4.2 SDK. However, it was not included in the 1.4.2 JRE release for Microsoft Windows. If you are running the 1.4.2 JRE for Windows and wish to use the GTK+ look and feel, you need to download the 1.4.2 SDK. We expect GTK+ to be shipped for all platforms in future JRE releases.


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

UIManager.getCrossPlatformLookAndFeelClassName()

Returns the look and feel that works on all platformsthe Java look and feel.

UIManager.getSystemLookAndFeelClassName()

Specifies the look and feel for the current platform. On Microsoft Windows platforms, this specifies the Windows look and feel. On Mac OS platforms, this specifies the Mac OS look and feel. On UNIX platforms, such as Solaris, or Linux this returns the CDE/Motif look and feel.

"com.sun.java.swing.plaf.gtk.GTKLookAndFeel"

Specifies the GTK+ look and feel introduced in release 1.4.2. You can specify the particular theme either using a resource file [74] or the gtkthemefile command-line parameter. Here's an example:

[74] More information on resource files is at: http://developer.gnome.org/doc/API/2.0/gtk/gtk-Resource-Files.html.

 java -Dswing.gtkthemefile=customTheme/gtkrc Application 
"javax.swing.plaf.metal.MetalLookAndFeel"

Specifies the Java look and feel. (The code name was Metal .)

"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"

Specifies the Windows look and feel. Currently, you can use this only on Microsoft Windows systems.

Version Note: As of release 1.4.2, WindowsLookAndFeel has been updated to mimic the Windows XP look and feel when running on the Windows XP platform.


"com.sun.java.swing.plaf.motif.MotifLookAndFeel"

Specifies the CDE/Motif look and feel. This can be used on any platform.

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

Specifying the Look and Feel: Command Line

You can specify the look and feel at the command line by using the -D flag to set the swing.defaultlaf property. For example:

 java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel MyApp java -Dswing.defaultlaf= \      com.sun.java.swing.plaf.windows.WindowsLookAndFeel MyApp 

Specifying the Look and Feel: swing.properties

Yet another way to specify the current look and feel is to use the swing.properties file to set the swing.defaultlaf property. This file is located in the lib directory of the J2SE release. If you're using the Java interpreter in javaHomeDirectory \bin , for example, the swing.properties file (if it exists) is in javaHomeDirectory \lib . Here's an example of the contents of a swing.properties file:

 # Swing properties swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel 

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 it's successful, all components use that look and feel.

  2. If the program hasn't successfully specified a look and feel, the UI manager uses the one specified by the swing.defaultlaf property. If the property is specified in the swing.properties file and on the command line, the command-line definition takes precedence.

  3. If none of these steps has resulted in a valid look and feel, the program uses the Java look and 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 change, invoke the SwingUtilities updateComponentTreeUI method once per top-level container. Then you can resize each top-level container to reflect the new sizes of its contained components. For example:

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


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net