Java Reference
In-Depth Information
Object iconObject =
LookAndFeel.makeIcon(LazySample.class, "World.gif");
UIManager.put("Tree.leafIcon", iconObject);
Integer fifteen = new Integer(15);
Object lazyArgs[] =
new Object[] {Color.GREEN, Boolean.TRUE, fifteen, fifteen};
Object lazyDiamond =
new UIDefaults.ProxyLazyValue("DiamondIcon", lazyArgs);
UIManager.put("Tree.openIcon", lazyDiamond);
JTree tree = new JTree();
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(200, 200);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Unlike lazy values, active values act like instance-creation factories. Every time they're
asked for a value with one of the get() methods of UIManager , a new instance is created and
returned. The interface method is the same as that for the UIDefault.LazyValue interface; only
the interface name is different.
public interface UIDefaults.ActiveValue {
public Object createValue(UIDefaults table);
}
To demonstrate, Listing 20-5 defines a factory that constructs JLabel components. The
text of the label will function as a counter to show how many labels have been created. When-
ever the createValue() method is called, a new JLabel is created.
Listing 20-5. Active Label Factory
import javax.swing.*;
public class ActiveLabel implements UIDefaults.ActiveValue {
private int counter = 0;
public Object createValue(UIDefaults defaults) {
JLabel label = new JLabel(""+counter++);
return label;
}
}
Search WWH ::




Custom Search