![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Painting the JSliders Track For some types of controls, such as a volume control, you dont want to paint the sliders track. The paintTrack property controls this behavior. Passing the setPaintTrack method false will stop the slider from painting the track. As you have seen from the previous examples in this chapter, the default value for this property is true. The getPaintTrack method can query the current setting of this property. The paintTrack property is bound in the JSlider class. Adding the following line of code to the PercentSliderTest application, shown in the previous section, will disable the track from being drawn in the slider: slider.setPaintTrack( false ); The resulting slider is shown in Figure 10.15.
Displaying the JSliders Value Up to this point, you have displayed and configured JSlider instances, displayed tick marks, and displayed labels for the range of the slider. However, you have not been able to display the current value of the slider. Most of the time, this is useful. It is very rare that a slider is used to alter a value without showing the user the current value selected in the slider. Unfortunately, there is no setPaintValue method in the JSlider class. Thus, to show the current value of the slider, you must do some work. The JSlider class again could be extended to display the current value somewhere in its space. However, this is not the approach youll take here. Instead, a panel is created that contains the slider and a label for the current value. The panel will be a mediator between the slider and label. It will listen for change events from the slider and update the label when such events are received. The SliderValuePanel class is shown in Listing 10.20. In its constructor, the slider and label are created. The slider is configured to paint tick marks and labels. A JLabel is created to display the current value in the slider. The class registers itself as a ChangeListener to the slider so that its notified when the value in the slider changes. The stateChanged method being called performs this function. This method queries the current value of the slider and writes that value in the label. The panel is configured with a BorderLayout manager. The slider is placed in the center region and the label in the south. Listing 10.20 The SliderValuePanel Class package com.foley.utility; import java.awt.*; import javax.swing.*; import javax.swing.event.*; /** * The SliderValuePanel class is a panel that contains a * slider and a label to display the current value in the slider. * <p> * The current implementation requires the user to get the slider * to configure it. Wrapper methods for the BoundedRangeModel * method should be provided. * <p> * @author Mike Foley **/ public class SliderValuePanel extends JPanel implements ChangeListener { /** * Components in the panel. **/ private JSlider slider; private JLabel label; /** * The String that the current value is appended to. **/ private static final String labelBase = Current Value: ; /** * SliderValuePanel, constructor * <p> * Create the JSlider instance and JLabel instance * and arrange them in the panel. **/ public SliderValuePanel() { super(); setLayout( new BorderLayout() ); slider = new JSlider(); slider.setMajorTickSpacing( 20 ); slider.setMinorTickSpacing( 5 ); slider.setPaintTicks( true ); slider.setPaintLabels( true ); label = new JLabel( labelBase + slider.getValue() ); add( slider, BorderLayout.CENTER ); add( label, BorderLayout.SOUTH ); slider.addChangeListener( this ); } /** * Return the slider in this panel. This allows * clients to get a handle to the slider and configure * it. * <p> * @return The slider in this panel. **/ public JSlider getSlider() { return( slider ); } /** * stateChanged, from ChangeListener * <p> * The slider in the panel value has changed. * Update the labels text. * <p> * @param event The event causing this method to be called. **/ public void stateChanged( ChangeEvent event ) { label.setText( labelBase + slider.getValue() ); } } // SliderValuePanel
|
![]() |
|