Changing a Swing Program’s Look and Feel
Problem
You want to change the look and feel of an application.
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 UIManager.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 this book; refer to the book
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 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. You call setLookAndFeel( )
as previously, and then call the static
SwingUtilities.updateComponentTree( )
for your JFrame
and
all detached trees, such as dialog classes. But before you rush out
to do it, please be advised that the official Sun position is that
you shouldn’t! The official Java Look and Feel
Design Guideline book says, on page 23 (first edition):
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 ...
Get Java Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.