Change Listeners

To monitor slider input, you must have a class that implements the ChangeListener interface in the javax.swing.event package. This interface includes only one method:

public void stateChanged(ChangeEvent event); {    // statements to handle the event}

To register an object as a change listener, call the addChangeListener(Object) method of the container that holds the slider. When the slider is moved, the listening object’s stateChanged() method is called.

This method is called with a ChangeEvent object that can identify the slider component that changed in value. Call the object’s getSource() method and cast the object to a JSlider, as in the following statement:

JSlider changedSlider = (JSlider) event.getSource();

In this example, ...

Get Sams Teach Yourself Java™ in 24 Hours, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.