The JSlider Class

The JSlider class represents a graphical slider, a new component in Swing. Like scrollbars, sliders can have either a horizontal or a vertical orientation. With sliders, however, you can enhance their appearance with both tick marks and labels. The class hierarchy is illustrated in Figure 6.8.

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 look-and-feels, 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 field in the Dictionary consists of two fields: an integer key, which tells the index to draw the various component, followed by the component itself. If you do not wish to create your own label components, you can use the createStandardLabels() method to create a series of ...

Get Java Swing 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.