![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Change Event and Listener InterfaceThe ChangeListener interface defines the responsibilities of listeners to a BoundedRangeModel instance. This interface is shown below. The listener must implement one method, the stateChanged method. This method is called when any of the properties in the bounded range model change. The parameter to the stateChanged method is a ChangeEvent instance. This event is also shown below: public interface ChangeListener extends EventListener { void stateChanged(ChangeEvent e); } public class ChangeEvent extends EventObject { public ChangeEvent(Object source) { super(source); } } As you can see from the ChangeEvent class, it does not deliver any information specific to BoundedRangeModel. Thus, the listener must query the source of the event from the ChangeEvent and query the model for its current state. Example usages of the ChangeListener interface and ChangeEvent class are presented in the next section. The JSlider ControlThe JSlider class implements a control that allows the user to select a value from a bounded range. A pointer is drawn, which the user can slide to select the desired value. The JSlider class employs a BoundedRangeModel to manage its range and the current value. A JSlider instance can be created and displayed using the SliderTest application, shown in Listing 10.17. The resulting slider is shown in Figure 10.11. Listing 10.17 The SliderTest Application package com.foley.test; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import com.foley.utility.ApplicationFrame; /** * An application that displays a JSlider instance * in its frame. * * @author Mike Foley **/ public class SliderTest 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( Slider Test ); JSlider slider = new JSlider(); slider.setBorder( BorderFactory.createLoweredBevelBorder() ); frame.getContentPane().add( slider, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); } // main } // SliderTest
Calling the default constructor of the JSlider class creates a slider with a range from 0 to 100 and an initial value of 50. However, from looking at the slider in Figure 10.11, there is no way of knowing this. The slider can be configured to display the range and various tick mark options. The JSlider class also contains wrapper methods for the BoundedRangeModel methods. This allows the models properties to be set by calling methods on the slider itself rather than the model, if the developer is so inclined. The setOrientation method can be called for the slider to display vertically or horizontally. This method takes an integer parameter that must be one of the two orientation constants defined in the SwingConstants interface. These constants are HORIZONTAL and VERTICAL. An IllegalArgumentException is thrown if a parameter other than one of these two is passed to the method. The JSlider class implements the SwingConstants interface, so the constant can be specified as SwingConstants.VERTICAL or JSlider.VERTICAL. The JSlider class also contains a constructor that takes the orientation, minimum, maximum, and initial value for the slider. Calling the setInverted method with a parameter of true can be called to invert the slider. An inverted horizontal slider contains its maximum value to the left and its minimum value to the right, and a vertical slider contains its maximum value on the top and its minimum value on the bottom. The getInverted method will return the current state of the inverted property, which is a bound property of the JSlider class. The complete list of bound properties added by the JSlider is shown in Table 10.4.
|
![]() |
|