[ Pobierz całość w formacie PDF ]
learn more about that in Chapter 10.
UIDefaults Demonstration
There is one Hashtable subclass that is defined with the Swing classes: the UIDefaults class. It provides a hash
table for the UIManager to find the look-and-feel-dependent properties for the Swing components. It extends
the Hashtable functionality by storing default values in addition to the key-value object pairs. If you search
for a key that isn't in the hash table, the set of defaults is also searched. By changing a value in the UIDefaults
hash table, you effectively change the properties of the created Swing components. For instance, if you'd like
all JButton components to have white text with a red background and a 24-point italic Serif font, add the
following to the table:
UIManager.put("Button.foreground", Color.white);
63
Properties Basics
UIManager.put("Button.background", Color.red);
Font f = new Font("Serif", Font.ITALIC, 24);
UIManager.put("Button.font", f);
Figure 5-4 shows what a bunch of buttons with these defaults would look like. The actual buttons are created
simply by calling the constructor: new JButton(label).
Figure 5-4: An example of UIDefaults.
For each entry in the UIDefaults table, there is a string key. The value can be an instance of any type. For a
complete list of keys, see Appendix A of my book, Definitive Guide to Swing for Java 2, Second Edition
(Apress, 2000).
Properties Basics
The Properties class represents yet another specialized Hashtable. Instead of being a collection of key-value
pairs of any object, it is customized such that both the keys and the values are only supposed to be strings.
However, since this is a subclass of Hashtable, you can call the Hashtable methods directly to store other
object types. However, this should be avoided so that the listing, loading, and saving methods work as
expected. You'll see more on these capabilities shortly.
Warning Using the Hashtable methods to store nonstring objects in a Properties table will result in the store()
method throwing a ClassCastException and the getProperty() method returning null. The Properties
class doesn't just call the nonstring objects' toString() method to treat them as strings.
Table 5-3: Summary of the Properties Class
VARIABLE/METHOD NAME VERSION DESCRIPTION
Properties() 1.0 Constructs a properties list.
getProperty() 1.0 Retrieves a value for a key in the
properties list.
list() 1.0 Lists all the properties and their values.
load() 1.0 Loads the properties list from a stream.
propertyNames() 1.0 Returns a collection of the keys in the
properties list.
setProperty() 1.2 Places a key-value pair into the properties
list.
store() 1.2 Saves the properties list to a stream.
defaults 1.0 A default set of the property values
You can create properties with or without a set of default values. The default values are another set of
properties used when you look up values. If you haven't yet stored a value for a key in the new properties list,
the value for the key from the defaults set will be returned.
64
Using Properties
public Properties()
public Properties(Properties defaults)
Instances of the Properties class are both sized and grow like hash tables. If you wish to presize your
Properties instance, you cannot; there is no way to change the initial capacity or load factor for the hash table
used.
Using Properties
The two primary tasks of working with properties are setting and getting. You can also load them from an
input stream or save them to an output stream.
Setting and Getting Elements
The Properties class has a specialized setProperty() method to ensure both the key and the value are strings.
public Object setProperty(String key, String value)
Keys can consist of any Unicode characters except those listed in Table 5-4, which represent separator
characters the Properties class uses when its content is written to external storage. Also, keys cannot begin
with '#' or '!' these characters are reserved to represent comment lines in the saved properties files. However,
'#' and '!' are valid at other positions.
Table 5-4: Invalid Characters in Keys for Properties
CHARACTER REPRESENTATION
Equals Sign =
Colon :
Space
Tab \t
Newline \n
Return \r
Formfeed \f
[ Pobierz całość w formacie PDF ]