14.7. The JSlider Component
In the AWT, the Scrollbar class did double duty as a control for interactively selecting numeric values and as a widget used to control scrolling. This double duty resulted in poor-looking sliders. Swing gives you a “real” slider: JSlider. You create a JSlider in a manner similar to creating a Scrollbar: the zero-argument constructor creates a horizontal slider with a range from 0 to 100 and an initial value of 50. You can also supply the orientation (through JSlider.HORIZONTAL or JSlider.VERTICAL), the range, and initial value to the constructor. You handle events by attaching a ChangeListener. The JSlider stateChanged method normally calls getValue to look up the current JSlider value.