sliderV.setPreferredSize( new Dimension( 300, 50) );
As the user drags the knob across a slider change events are generated. The frequency of events depends on how fast the user drags the knob. A slow drag across the control results in an event generated for every new integer position of the knob. An event listener must potentially deal with many events everytime the user adjusts the slider.
A listener for a slider implements the
This interfaces has a single method:
public void stateChanged(ChangeEvent evt)
An object that implements the interface must be registered with the slider in order to receive events:
slider.addChangeListener( ChangeListener lstn )
Often, especially in small projects, the frame that contains the slider is also its listener.
Say that you have a frame class
that contains a slider and implements
Register an object of that class with its slider:
sliderV = new JSlider( SwingConstants.VERTICAL, 0, 1000, 400); sliderV.setMajorTickSpacing( 100 ); sliderV.setMinorTickSpacing( 50 ); sliderV.setPaintTicks ( true ); sliderV.setPaintLabels( true ); sliderV.addChangeListener( ______ );
Hint: this is the same as we have been doing with frames that
ActionListener and their buttons.