Change Listeners

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, as in the following statement:

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

In this example, evt is the ChangeEvent object that was an argument to the stateChanged() method.

When you are using a change listener, it's important to note that change events occur throughout the movement of a slider. They begin when the slider is first moved, and don't stop occurring until the slider has been released.

For this reason, you might not want to do anything in the stateChanged() method until the slider has stopped moving.

To see whether a slider is currently being moved around, call its getValueIsAdjusting() method. This method returns true while movement is taking place, and false otherwise.

This technique is demonstrated in your next project, a Java application that uses three sliders to choose a color. Colors are created in Java by using the Color class, which is part of the java.awt package.

One way to create a Color object is to specify the amount of red, green, and blue in the color. Each of these can be an integer from 0 to 255, with 255 representing the maximum amount of that color.

For example, the following statement creates a Color object that represents the color butterscotch:

Color butterscotch = new Color(255, 204, 128);

The red value used to create this Color object is 255, so it contains the maximum amount of red. It also contains a large amount of green and some blue.

Colors can be represented by a range of values, so they are an ideal use for slider components.

Listing 16.2 contains the ColorSlide application, which has three sliders, three labels for the sliders, and a panel where the color is displayed. Enter this source code into your word processor and save the file as

Listing 16.2. The Full Text of

 1: import javax.swing.*;

 2: import javax.swing.event.*;

 3: import java.awt.*;


 5: public class ColorSlide extends JFrame implements ChangeListener {

 6:     ColorPanel canvas = new ColorPanel();

 7:     JSlider red = new JSlider(0, 255, 255);

 8:     JSlider green = new JSlider(0, 255, 0);

 9:     JSlider blue = new JSlider(0, 255, 0);


 11:     public ColorSlide() {

 12:         super("Color Slide");

 13:         setSize(270, 300);

 14:         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 15:         setVisible(true);


 17:         red.setMajorTickSpacing(50);

 18:         red.setMinorTickSpacing(10);

 19:         red.setPaintTicks(true);

 20:         red.setPaintLabels(true);

 21:         red.addChangeListener(this);


 23:         green.setMajorTickSpacing(50);

 24:         green.setMinorTickSpacing(10);

 25:         green.setPaintTicks(true);

 26:         green.setPaintLabels(true);

 27:         green.addChangeListener(this);


 29:         blue.setMajorTickSpacing(50);

 30:         blue.setMinorTickSpacing(10);

 31:         blue.setPaintTicks(true);

 32:         blue.setPaintLabels(true);

 33:         blue.addChangeListener(this);


 35:         JLabel redLabel = new JLabel("Red: ");

 36:         JLabel greenLabel = new JLabel("Green: ");

 37:         JLabel blueLabel = new JLabel("Blue: ");

 38:         GridLayout grid = new GridLayout(4, 1);

 39:         FlowLayout right = new FlowLayout(FlowLayout.RIGHT);

 40:         setLayout(grid);


 42:         JPanel redPanel = new JPanel();

 43:         redPanel.setLayout(right);

 44:         redPanel.add(redLabel);

 45:         redPanel.add(red);

 46:         add(redPanel);


 48:         JPanel greenPanel = new JPanel();

 49:         greenPanel.setLayout(right);

 50:         greenPanel.add(greenLabel);

 51:         greenPanel.add(green);

 52:         add(greenPanel);


 54:         JPanel bluePanel = new JPanel();

 55:         bluePanel.setLayout(right);

 56:         bluePanel.add(blueLabel);

 57:         bluePanel.add(blue);

 58:         add(bluePanel);

 59:         add(canvas);


 61:         setVisible(true);

 62:     }


 64:     public void stateChanged(ChangeEvent evt) {

 65:         JSlider source = (JSlider)evt.getSource();

 66:         if (source.getValueIsAdjusting() != true) {

 67:             Color current = new Color(red.getValue(), green.getValue(),

 68:                 blue.getValue());

 69:             canvas.changeColor(current);

 70:             canvas.repaint();

 71:         }

 72:     }


 74:     public Insets getInsets() {

 75:         Insets border = new Insets(45, 10, 10, 10);

 76:         return border;

 77:     }


 79:     public static void main(String[] arguments) {

 80:         ColorSlide cs = new ColorSlide();

 81:     }

 82: }


 84: class ColorPanel extends JPanel {

 85:     Color background;


 87:     ColorPanel() {

 88:         background =;

 89:     }


 91:     public void paintComponent(Graphics comp) {

 92:         Graphics2D comp2D = (Graphics2D)comp;

 93:         comp2D.setColor(background);

 94:         comp2D.fillRect(0, 0, getSize().width, getSize().height);

 95:     }


 97:     void changeColor(Color newBackground) {

 98:         background = newBackground;

 99:     }

100: }

Compile and run the application. A frame will open that contains three sliders that represent the amount of red, green, and blue in a panel along the bottom edge of the frame.

Adjust the values of each slider to change the color that is displayed. Figure shows the application being used to create North Texas Mean Green, a color that has a red value of 50, a green value of 150, and a blue value of 50. This shade inspires students and alumni of the University of North Texas, the football juggernaut of the Sun Belt Conference, to leap to our feet and make ferocious eagle-claw hand gestures that put fear in our rivals.

Perhaps it loses some of its verdant ferociousness in black-and-white.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows