JScrollBar is a component that enables the user to select from a range of values, as shown in Figure 15.26.
Normally, the user changes the value of the scroll bar by making a gesture with the mouse. For example, the user can drag the scroll bar's bubble up and down, or click in the scroll bar's unit-increment or block-increment areas. Keyboard gestures can also be mapped to the scroll bar. By convention, the Page Up and Page Down keys are equivalent to clicking in the scroll bar's block-increment and block-decrement areas.
Note
The width of the scroll bar's track corresponds to maximum + visibleAmount . When a scroll bar is set to its maximum value, the left side of the bubble is at maximum , and the right side is at maximum + visibleAmount . |
JScrollBar has the following properties, as shown in Figure 15.27.
Normally, a program changes a scroll bar's value by calling the setValue method. The setValue method simultaneously and synchronously sets the minimum, maximum, visible amount, and value properties of a scroll bar, so that they are mutually consistent.
When the user changes the value of the scroll bar, the scroll bar generates an instance of AdjustmentEvent , which is passed to every registered listener. An object that wishes to be notified of changes to the scroll bar's value must implement the adjustmentValueChanged method in the AdjustmentListener interface defined in the package java.awt.event .
Listing 15.9 gives a program that uses horizontal and vertical scroll bars to control a message displayed on a panel. The horizontal scroll bar is used to move the message to the left or the right, and the vertical scroll bar to move it up and down. A sample run of the program is shown in Figure 15.28.
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 scroll bar and place it in the east of the frame. Create a horizontal scroll bar and place it in the south of the frame. |
| |
2. | Process the event. Create a listener to implement the adjustmentValueChanged handler to move the message according to the bar movement in the scroll bars. |
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class ScrollBarDemo extends JFrame { 6 // Create horizontal and vertical scroll bars 7 private JScrollBar jscbHort = 8 new JScrollBar(JScrollBar.HORIZONTAL); 9 private JScrollBar jscbVert = 10 new JScrollBar(JScrollBar.VERTICAL); 11 12 // Create a MessagePanel 13 private MessagePanel messagePanel = 14 new MessagePanel( "Welcome to Java" ); 15 16 public static void main(String[] args) { 17 ScrollBarDemo frame = new ScrollBarDemo(); 18 frame.setTitle( "ScrollBarDemo" ); 19 frame.setLocationRelativeTo( null ); // Center the frame 20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 21 frame.pack(); 22 frame.setVisible( true ); 23 } 24 25 public ScrollBarDemo() { 26 // Add scroll bars and message panel to the frame 27 setLayout( new BorderLayout()); 28 add(messagePanel, BorderLayout.CENTER); 29 add(jscbVert, BorderLayout.EAST); 30 add(jscbHort, BorderLayout.SOUTH); 31 32 // Register listener for the scroll bars 33 jscbHort.addAdjustmentListener( new AdjustmentListener() { 34 public void adjustmentValueChanged(AdjustmentEvent e) { 35 // getValue() and getMaximumValue() return int, but for better 36 // precision, use double 37 double value = jscbHort.getValue(); 38 double maximumValue = jscbHort.getMaximum(); 39 double newX = (value * messagePanel.getWidth() 40 / maximumValue); 41 messagePanel.setXCoordinate(( int ) newX); 42 } 43 }); 44 jscbVert.addAdjustmentListener( new AdjustmentListener() { 45 public void adjustmentValueChanged(AdjustmentEvent e) { 46 // getValue() and getMaximumValue() return int, but for better 47 // precision, use double 48 double value = jscbVert.getValue(); 49 double maximumValue = jscbVert.getMaximum(); 50 double newY = (value * messagePanel.getHeight() 51 / maximumValue); 52 messagePanel.setYCoordinate(( int )newY); 53 } 54 }); 55 } 56 } |
The program creates two scroll bars ( jscbVert and jscbHort ) (lines 7 “10) and an instance of MessagePanel ( messagePanel ) (lines 13 “14). messagePanel is placed in the center of the frame; jscbVert and jscbHort are placed in the east and south sections of the frame (lines 29 “30), respectively.
You can specify the orientation of the scroll bar in the constructor or use the setOrientation method. By default, the property value is 100 for maximum , for minimum , 10 for blockIncrement , and 10 for visibleAmount .
When the user drags the bubble, or clicks the increment or decrement unit, the value of the scroll bar changes. An instance of AdjustmentEvent is generated and passed to the listener by invoking the adjustmentValueChanged handler. The listener for the vertical scroll bar moves the message up and down (lines 34 “42), and the listener for the horizontal bar moves the message to right and left (lines 43 “53).
The maximum value of the vertical scroll bar corresponds to the height of the panel, and the maximum value of the horizontal scroll bar corresponds to the width of the panel. The ratio between the current and maximum values of the horizontal scroll bar is the same as the ratio between the x value and the width of the message panel. Similarly, the ratio between the current and maximum values of the vertical scroll bar is the same as the ratio between the y value and the height of the message panel. The x -coordinate and y -coordinate are set in response to the scroll bar adjustments (lines 39, 50).