![]() |
|||
![]() ![]() |
![]() |
|
![]() |
The SliderValuePanel easily could be enhanced so that the position of the label relative to the slider would be a property of the panel, rather than hard coded as it currently is. It would also be useful to allow the slider to be set. This would allow enhanced sliders, such as the PercentSlider presented earlier in the chapter, to be used with the SliderValuePanel class. A final enhancement would be to change the label to a JTextField so the user could enter the desired value directly. This would require the panel to listen to changes in the textfield and update the slider after the user entered a new value. The SliderValuePanelTest application, shown in Listing 10.21, is used to test the SliderValuePanel class. This application creates the SliderValuePanel instance and adds it to a frame for display. The resulting window is shown in Figure 10.16. When this application is executed, the slider can be dragged, and the label will update as the slider moves to display the current value in the slider. Listing 10.21 The SliderValuePanelTest Application package com.foley.test; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import com.foley.utility.ApplicationFrame; import com.foley.utility.SliderValuePanel; /** * An application that displays a SliderValuePanelTest instance * in its frame. * * @author Mike Foley **/ public class SliderValuePanelTest extends Object { /** * Application entry point. * Create a frame, the list and display it. * * @param args Command line parameter. Not used. **/ public static void main( String args[] ) { JFrame frame = new ApplicationFrame( SliderValuePanel Test ); SliderValuePanel sliderValuePanel = new SliderValuePanel(); sliderValuePanel.setBorder( BorderFactory.createLoweredBevelBorder() ); frame.getContentPane().add( sliderValuePanel, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); } // main } // SliderValuePanelTest
The JScrollBar ClassThe JScrollBar class is used internally by JFC classes. However, it can be instantiated and used directly by user classes as well. Like the JSlider class, the JScrollBar class employs a BoundedRangeModel to manage its range and current value. In fact, as you saw during the discussion of the BoundedRangeModel interface, some of its properties are designed with a scrollbar in mind. The extent property comes to mind. The JScrollBar class contains a wrapper method for the properties of BoundedRangeModel. These methods are convenience methods that forward their parameters to the model. The JScrollBar class also implements the java.awt.Adjustable interface. As such, it must contain the methods defined in that interface. The bound properties introduced by the JScrollBar class are presented in Table 10.5.
Note that the constants used as the parameters for the setOrientation method of the JScrollBar class are defined in the AWT Adjustable interface that JScrollBar implements. Earlier in this chapter, you saw constants with the exact same names and purposes used in the JSlider class defined in the SwingConstants class. Having constants with identical names and meanings in multiple locations is a dangerous habit. Luckily, the constants are defined with the same values so they can be used interchangeably. We can only hope that as AWT and JFC mature, this type of overlap will be eliminated. You saw the JScrollBar class in action earlier in this chapter in the JList class. You will also see it in later chapters when the JScrollPane class is explored.
|
![]() |
|