Using Other Swing Features |
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.
- How to Set the Look and Feel
- How the UI Manager Chooses the Look and Feel
- Changing the Look and Feel After Startup
How to Set the Look and 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 could 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. At press time, this look and feel was in beta release and not available as part of either JFC 1.1 or JDK 1.2. Instead, you download it, following the instructions on the JFC Home Page.
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. For example, although the Mac OS Look & Feel isn't included in JDK 1.2, you can use it if you add it to your application'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:
- If the program sets the look and feel before any components are created, the UI manager tries to create an instance of the specified class. If successful, all components use that look and feel. [PENDING: check]
- 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:For more information about Swing properties, see [PENDING: where?]# 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.
Changing the Look and Feel After Startup
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();
Using Other Swing Features |