Java Reference
In-Depth Information
Snapping the JSlider Thumb into Position
One additional property of JSlider is related to tick marks: the snapToTicks property, set with
public void setSnapToTicks(boolean newValue) . When this property is true and tick marks are
displayed, after you move the slider's thumb, the thumb will rest only on a tick. For instance, if
a slider has a range of 0-100 with tick marks at every tenth unit, and you drop the thumb at the
33 mark, the thumb will snap to the position of the tick at 30. If tick marks aren't displayed, the
property setting has no effect, including when labels are displayed without tick marks.
Labeling JSlider Positions
As Figure 12-5 (shown earlier in the chapter) demonstrates, you can label any position within
the JSlider with a Component . When a position is labeled, the component will be displayed next
to it. The labels are stored within a lookup table that subclasses the Dictionary class, where the
key is the Integer position and the value is the Component . Any AWT Component can be the label;
however, the JLabel is best suited to the role. Figure 12-7 shows how the dictionary for the right
slider of Figure 12-5 might look.
Figure 12-7. A JSlider dictionary
Search WWH ::




Custom Search