go to previous page   go to home page   go to next page


sliderV.setPreferredSize( new Dimension( 50, 300) ); 


Bouncing Change

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 ChangeListener interface. 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 ChangeListener. Register an object of that class with its slider:

sliderV = new JSlider( JSlider.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 implement ActionListener and their buttons.