Java Reference
In-Depth Information
Solution
Use the static
UIManager.setLookAndFeel()
method. Maybe.
Discussion
If you wish to specify the entire look and feel for a program, set it with the static
UIMan-
ager.setLookAndFeel()
method; the name you pass in must be the full name (as a string)
of a class that implements a Java look and feel. The details of writing a look and feel class
are beyond the scope of this topic; refer to
Java Swing
or the Sun documentation. But using
these classes is easy. For example:
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
This must appear before you create the GUI of the program, and it can throw an exception if
the class name is invalid.
People sometimes like to show off the fact that you can change the look and feel on the fly.
As earlier, you call
setLookAndFeel()
, and then call the static
SwingUtilit-
ies.updateComponentTree()
for your
JFrame
and all detached trees, such as dialog
classes. But before you rush out to do it, be advised that the official Sun position is that you
shouldn't! The first edition of the official
Java Look and Feel Design Guideline
book says,
on page 23 (Oracle):
Because there is far more to the design of an application than the look and feel of components,
it is unwise to give end users the ability to swap look and feel while [running] your application.
Switching look and feel designs in this way only swaps the look and feel designs from one plat-
form to another. The layout and vocabulary used are platform-specific and do not change. For
instance, swapping look and feel designs does not change the titles of the menus.
The topic does recommend that you let users specify an alternative look and feel, presumably
in your properties file, at program startup time. Even so, the capability to switch while an ap-
plication is running is too tempting to ignore; even Sun's own Swing Demonstration (in-
cluded with the JDK) offers a menu item to change its look and feel.
Figure 14-13
is my nice
little program in the Java style; see
Example 14-10
for the source code.