JSlider is similar to JScrollBar , but JSlider has more properties and can appear in many forms. Figure 15.29 shows two sliders.
JSlider lets the user graphically select a value by sliding a knob within a bounded interval. The slider can show both major tick marks and minor tick marks between them. The number of pixels between the tick marks is controlled by the majorTickSpacing and minorTickSpacing properties. Sliders can be displayed horizontally or vertically, with or without ticks , and with or without labels. The frequently used constructors and properties in JSlider are shown in Figure 15.30.
Note
The values of a vertical scroll bar increase from top to bottom, but the values of a vertical slider decrease from top to bottom. |
Note
All the properties listed in Figure 15.30 have the associated get and set methods , which are omitted for brevity. By convention, the get method for a Boolean property is named is<PropertyName>() . In the JSlider class, the get methods for paintLabels , paintTicks() , paintTrack() , and inverted are getPaintLabels() , getPaintTicks() , getPaintTrack() , and getInverted() , which violate the naming convention. |
When the user changes the value of the slider, the slider generates an instance of javax.swing.event.ChangeEvent , which is passed to any registered listeners. Any object that wishes to be notified of changes to the slider's value must implement stateChanged method in the ChangeListener interface defined in the package javax.swing.event .
Listing 15.10 writes a program that uses the sliders to control a message displayed on a panel, as shown in Figure 15.29. Here are the major steps in the program:
1. | Create the user interface. Create a MessagePanel object and place it in the center of the frame. Create a vertical slider and place it in the east of the frame. Create a horizontal slider and place it in the south of the frame. |
2. | Process the event. Create a listener to implement the stateChanged handler in the ChangeListener interface to move the message according to the knot movement in the slider. |
1 import java.awt.*; 2 import javax.swing.*; 3 import javax.swing.event.*; 4 5 public class SliderDemo extends JFrame { 6 // Create horizontal and vertical sliders 7 private JSlider jsldHort = new JSlider(JSlider.HORIZONTAL); 8 private JSlider jsldVert = new JSlider(JSlider.VERTICAL); 9 10 // Create a MessagePanel 11 private MessagePanel messagePanel = 12 new MessagePanel( "Welcome to Java" ); 13 14 public static void main(String[] args) { 15 SliderDemo frame = new SliderDemo(); 16 frame.setTitle( "SliderDemo" ); 17 frame.setLocationRelativeTo( null ); // Center the frame 18 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 frame.pack(); 20 frame.setVisible( true ); 21 } 22 23 public SliderDemo() { 24 // Add sliders and message panel to the frame 25 setLayout( new BorderLayout( 5 , 5 )); 26 add(messagePanel, BorderLayout.CENTER); 27 add(jsldVert, BorderLayout.EAST); 28 add(jsldHort, BorderLayout.SOUTH); 29 30 // Set properties for sliders 31 jsldHort.setMaximum( 50 ); 32 jsldHort.setPaintLabels( true ); 33 jsldHort.setPaintTicks( true ); 34 jsldHort.setMajorTickSpacing( 10 ); 35 jsldHort.setMinorTickSpacing( 1 ); 36 jsldHort.setPaintTrack( false ); 37 jsldVert.setInverted( true ); 38 jsldVert.setMaximum( 10 ); 39 jsldVert.setPaintLabels( true ); 40 jsldVert.setPaintTicks( true ); 41 jsldVert.setMajorTickSpacing( 10 ); 42 jsldVert.setMinorTickSpacing( 1 ); 43 44 // Register listener for the sliders 45 jsldHort.addChangeListener(new ChangeListener() { 46 /** Handle scroll bar adjustment actions */ 47 public void stateChanged(ChangeEvent e) { 48 // getValue() and getMaximumValue() return int, but for better 49 // precision, use double 50 double value = jsldHort.getValue(); 51 double maximumValue = jsldHort.getMaximum(); 52 double newX = (value * messagePanel.getWidth() 53 / maximumValue); 54 messagePanel.setXCoordinate(( int )newX); 55 } 56 }); 57 jsldVert.addChangeListener( new ChangeListener() { 58 /** Handle scroll bar adjustment actions */ 59 public void stateChanged(ChangeEvent e) { 60 // getValue() and getMaximumValue() return int, but for better 61 // precision, use double 62 double value = jsldVert.getValue(); 63 double maximumValue = jsldVert.getMaximum(); 64 double newY = (value * messagePanel.getHeight() 65 / maximumValue); 66 messagePanel.setYCoordinate(( int )newY); 67 } 68 }); 69 } 70 } |
JSlider is similar to JScrollBar , but JSlider has more features. As shown in this example, you can specify maximum, labels, major ticks, and minor ticks on a JSlider (lines 31 “35). You can also choose to hide the track (line 36). Since the values of a vertical slider decrease from top to bottom, the setInverted method reverses the order (line 37).
JSlider fires ChangeEvent when the slider is changed. The listener needs to implement the stateChanged handler in ChangeListener (lines 45 “69). Note that JScrollBar fires AdjustmentEvent when the scroll bar is adjusted.