Java Reference
In-Depth Information
Swing's lightweight GUI components eliminate many of these issues by providing
uniform functionality across platforms and by defining a uniform cross-platform look-
and-feel. Section 12.2 introduced the Nimbus look-and-feel. Earlier versions of Java used
the metal look-and-feel , which is still the default. Swing also provides the flexibility to cus-
tomize the look-and-feel to appear as a Microsoft Windows-style look-and-feel (only on
Windows systems), a Motif-style (UNIX) look-and-feel (across all platforms) or a Macin-
tosh look-and-feel (only on Mac systems).
Figures 22.9-22.10 demonstrate a way to change the look-and-feel of a Swing GUI.
It creates several GUI components, so you can see the change in their look-and-feel at the
same time. The output windows show the Metal, Nimbus, CDE/Motif, Windows and
Windows Classic look-and-feels that are available on Windows systems. The installed
look-and-feels will vary by platform.
We've covered the GUI components and event-handling concepts in this example
previously, so we focus here on the mechanism for changing the look-and-feel. Class
UIManager (package javax.swing ) contains nested class LookAndFeelInfo (a public
static class) that maintains information about a look-and-feel. Line 20 (Fig. 22.9)
declares an array of type UIManager.LookAndFeelInfo (note the syntax used to identify
the static inner class LookAndFeelInfo ). Line 34 uses UIManager static method get-
InstalledLookAndFeels to get the array of UIManager.LookAndFeelInfo objects that
describe each look-and-feel available on your system.
Performance Tip 22.1
Each look-and-feel is represented by a Java class. UIManager method getInstalled-
LookAndFeels does not load each class. Rather, it provides the names of the available look-
and-feel classes so that a choice can be made (presumably once at program start-up). This
reduces the overhead of having to load all the look-and-feel classes even if the program will
not use some of them.
1
// Fig. 22.9: LookAndFeelFrame.java
2
// Changing the look-and-feel.
3
import java.awt.GridLayout;
4
import java.awt.BorderLayout;
5
import java.awt.event.ItemListener;
6
import java.awt.event.ItemEvent;
7
import javax.swing.JFrame;
8
9
import javax.swing.UIManager;
import javax.swing.JRadioButton;
10
import javax.swing.ButtonGroup;
11
import javax.swing.JButton;
12
import javax.swing.JLabel;
13
import javax.swing.JComboBox;
14
import javax.swing.JPanel;
15
import javax.swing.SwingConstants;
16
17
18
import javax.swing.SwingUtilities;
public class LookAndFeelFrame extends JFrame
19
{
Fig. 22.9 | Look-and-feel of a Swing-based GUI. (Part 1 of 3.)
 
Search WWH ::




Custom Search