Change Listeners

To monitor the use of a slider in a Java program, you must have a class that implements the ChangeListener interface, which is part of the javax.swing.event package of classes. This interface includes only one method, which takes the following form:

public void stateChanged(ChangeEvent evt) {
    // 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 will be called.

This method is called with a ChangeEvent object that can be used to identify the slider component that was changed in value. Call the object's getSource() method and cast the object to a JSlider ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH EDITION now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.