Java Reference
In-Depth Information
Normally, the Dictionary used to store the labels is a Hashtable . However, any class that
extends the Dictionary class and that can use Integer keys will do. After you've created
your dictionary of labels, you associate the dictionary with the slider with the public void
setLabelTable(Dictionary newValue) method. The following source creates the label lookup
table associated with Figure 12-7.
Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
table.put (0, new JLabel(new DiamondIcon(Color.RED)));
table.put (10, new JLabel("Ten"));
table.put (25, new JLabel("Twenty-Five"));
table.put (34, new JLabel("Thirty-Four"));
table.put (52, new JLabel("Fifty-Two"));
table.put (70, new JLabel("Seventy"));
table.put (82, new JLabel("Eighty-Two"));
table.put (100, new JLabel(new DiamondIcon(Color.BLACK)));
aJSlider.setLabelTable (table);
Note Keep in mind that with J2SE 5.0, the compiler will auto-box an int parameter into an Integer .
Simply associating the label table with the slider won't display the labels. To enable their
painting, you need to call the public void setPaintLabels(boolean newValue) method with a
parameter of true . If you haven't manually created a table of labels, the system will create one
with an interval of values reflecting the major tick spacing. For example, the left slider of Figure
12-5 has a slider range of 0-100 and major tick spacing of 10. When setPaintLabels(true) is
called on that slider, labels are created for 0, 10, 20, and so on, all the way up to 100. The minor
tick spacing is irrelevant as far as automatic generation of labels goes. And the ticks don't need
to be painted for the labels to appear; the getPaintTicks() method can return false .
The automatic creation of labels is done through the public Hashtable createStandard
Labels(int increment) method, where the increment is the major tick spacing. You don't need
to call this method directly. If you want to create the labels from other than the minimum
value, you can call the overloaded public Hashtable createStandardLabels
(int increment, int start) variety, and associate the hash table with the slider yourself.
Customizing a JSlider Look and Feel
Each installable Swing look and feel provides a different JSlider appearance and set of default
UIResource values. Figure 12-8 shows the appearance of the JSlider component for the prein-
stalled set of look and feel types.
 
Search WWH ::




Custom Search