Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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.
To programmatically specify a look and feel, use theUIManager.setLookAndFeel
method. For example, the bold code in the following snippet makes the program use the Java Look & Feel:The argument topublic static void main(String[] args) { try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { } new SwingApplication(); //Create and show the GUI. }setLookAndFeel
is the fully qualified name of the appropriate subclass ofLookAndFeel
. To specify the Java Look & Feel, we used thegetCrossPlatformLookAndFeelClassName
method. If you want to specify the native look and feel for whatever platform the user runs the program on, usegetSystemLookAndFeelClassName
, 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.
Here are the look-and-feel determination steps that occur when the UI manager first initializes itself:
- 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.
- 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 thelib
directory of the Java release. For example, if you're using the Java interpreter injavaHomeDirectory\bin
, then theswing.properties
file (if it exists) is injavaHomeDirectory\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 aswing.properties
file:# Swing properties swing.defaultlaf=com.sun.java.swing.plaf.motif.MotifLookAndFeel
- If neither the program nor the user successfully specifies a look and feel, then the program uses the Java Look & Feel.
You can change the look and feel withsetLookAndFeel
even after the program's GUI is visible. To make existing components reflect the new look and feel, invoke theSwingUtilities
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
Feedback Form |
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.