Java Reference
In-Depth Information
the
defaults
property are equivalent. When you want to make changes to the look and feel, you
use the
defaults
property. That way, the settings for a predefined look and feel don't change.
UIManager.LookAndFeelInfo Class
When you ask the
UIManager
for the list of installed look and feel classes, you're returned an
array of
UIManager.LookAndFeelInfo
objects. From this array, you can find out the descriptive
name of the look and feel (from the
name
property of the
LookAndFeel
implementation), as well
as the class name for the implementation. As Table 20-5 shows, the two settings are read-only.
Table 20-5.
UIManager.LookAndFeelInfo Properties
Property Name
Data Type
Access
className
String
Read-only
name
String
Read-only
UIDefaults Class
The
LookAndFeel
classes and the
UIManager
use a special
UIDefaults
hash table to manage the
Swing component properties that depend on the look and feel. The special behavior is that
whenever a new setting is placed in the hash table with
put()
, a
PropertyChangeEvent
is generated
and any registered
PropertyChangeListener
objects are notified. Many of the
BasicLookAndFeel
classes automatically register the UI delegate to be interested in property change events at the
appropriate times.
If you need to change a number of properties at once, you can use the
public void
putDefaults(Object keyValueList[])
method, which causes only one notification event. With
putDefaults()
, the key/value entries alternate in a single-dimension array. For instance, to
cause buttons to have a default background color of pink and a foreground color of magenta,
you would use the following:
Object newSettings[] = {"Button.background", Color.PINK,
"Button.foreground", Color.MAGENTA};
UIDefaults defaults = UIManager.getDefaults();
defaults.putDefaults(newSettings);
Because
UIDefaults
is a
Hashtable
subclass, you can discover all the installed settings by using
an
Enumeration
to loop through all the keys or values. To simplify things a little, Listing 20-3
presents a program that lists the properties sorted within a
JTable
. It reuses several of the table
sorting classes from Chapter 18.
■
Note
Feel free to change the
UIDefaults
property lister program in Listing 20-3 to support modification
of property values.