The JSlider Class

The JSlider class represents a graphical slider. Like scrollbars, sliders can have either a horizontal or vertical orientation. With sliders, however, you can enhance their appearance with tick marks and labels. The class hierarchy is illustrated in Figure 6-8. In most instances, a slider is preferable to a standalone scrollbar. Sliders represent a selection of one value from a bounded range. Scrollbars represent a range of values within a bounded range and are best used in things like the JScrollPane.

JSlider class diagram

Figure 6-8. JSlider class diagram

The JSlider class allows you to set the spacing of two types of tick marks: major and minor. Major tick marks are longer than minor tick marks and are generally used at wider intervals. Figure 6-9 shows various sliders that can be composed in Swing.

Various sliders in Swing

Figure 6-9. Various sliders in Swing

The setPaintTicks( ) method sets a boolean, which is used to activate or deactivate the slider’s tick marks. In some L&Fs, the slider changes from a rectangular shape to a pointer when tick marks are activated. This is often done to give the user a more accurate representation of where the slider falls.

You can create a Dictionary of Component objects to annotate the slider. Each entry in the Dictionary consists of two fields: an Integer key, which supplies ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.