ScrollSlider.java Source code

Java tutorial

Introduction

Here is the source code for ScrollSlider.java

Source

/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski       
ISBN: 1-893115-78-X
Publisher: APress
*/
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

public class ScrollSlider {
    public static void main(String args[]) {
        JFrame f = new JFrame("Change Slider");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JSlider aJSlider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 500);
        ChangeListener aChangeListener = new BoundedChangeListener();
        aJSlider.addChangeListener(aChangeListener);
        Container c = f.getContentPane();
        c.add(aJSlider, BorderLayout.SOUTH);
        f.setSize(300, 200);
        f.setVisible(true);
    }
}

class BoundedChangeListener implements ChangeListener {
    public void stateChanged(ChangeEvent changeEvent) {
        Object source = changeEvent.getSource();
        if (source instanceof BoundedRangeModel) {
            BoundedRangeModel aModel = (BoundedRangeModel) source;
            if (!aModel.getValueIsAdjusting()) {
                System.out.println("Changed: " + aModel.getValue());
            }
        } else if (source instanceof JSlider) {
            JSlider theJSlider = (JSlider) source;
            if (!theJSlider.getValueIsAdjusting()) {
                System.out.println("Slider changed: " + theJSlider.getValue());
            }
        } else if (source instanceof JProgressBar) {
            JProgressBar theJProgressBar = (JProgressBar) source;
            System.out.println("ProgressBar changed: " + theJProgressBar.getValue());
        } else {
            System.out.println("Something changed: " + source);
        }
    }
}