Java Reference
In-Depth Information
Without the wrapped ColorUIResource constructor call, the color would remain pink after
a look and feel change.
Note Use of the specific UIResource implementation classes tends to be limited to those times when
you're creating a custom look and feel or customizing an existing one.
UIDefaults.ActiveValue, UIDefaults.LazyValue, and UIDefaults.ProxyLazyValue Classes
Besides implementing the UIResource interface, elements in the UIDefaults lookup table can
be lazy or active if they implement one of the inner classes of UIDefaults : LazyValue or ActiveValue .
For example, since Color and Dimension objects aren't very resource-intensive, when such an
element is placed in the UIDefaults table, the Color or Dimension is created and placed in the
table immediately—this is called active . On the other hand, in the case of a resource like an
Icon , and especially an ImageIcon , you want to defer creating and loading the icon class file
until it's needed—this is called lazy . Another example of an element you might want to make
lazy is a ListCellRenderer that needs a separate renderer for every JList component. Because
you don't know how many renderers you'll need or which renderer will be installed, you can
defer creation to a later time and get a unique version of the current renderer whenever you ask
for one.
Take a look at the public Object makeIcon(Class baseClass, String imageFile) method
of LookAndFeel . In order to handle the late loading of icon image files, the LookAndFeel class can
automatically create a LazyValue class for loading an Icon . Because the image file won't be
loaded until later, you need to provide the icon loader with the location of the icon image file
( baseClass ) and the file name ( imageFile ).
Object iconObject = LookAndFeel.makeIcon(this.getClass(), "World.gif");
UIManager.put("Tree.leafIcon", iconObject);
Next, look at the UIDefaults.LazyValue definition and create a lazy version of the DiamondIcon .
public interface UIDefaults.LazyValue {
public Object createValue(UIDefaults table);
}
In classes that implement the LazyValue interface, their constructors need to save any
information that will be passed along to the real constructor through the createValue() interface
method. To help with creating custom lazy values, the UIDefaults.ProxyLazyValue class provides a
way of saving this information to pass along. There are four ways to use ProxyLazyValue to defer
object creation, which each uses reflection to create the actual object, getting the specific how
(and what) from the constructor arguments:
public UIDefaults.ProxyLazyValue(String className) : If object creation will use the
no-argument constructor, just pass the class name as an argument.
public UIDefaults.ProxyLazyValue(String className, String method) : If object
creation will use a factory method that doesn't require arguments, pass the factory
method along with the class name.
 
Search WWH ::




Custom Search