import java.awt.BorderLayout;
import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollBar;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
public static void main(String args[]) throws Exception {
ChangeListener changeListener = new BoundedChangeListener();
JScrollBar anotherJScrollBar = new JScrollBar(JScrollBar.HORIZONTAL);
BoundedRangeModel model = anotherJScrollBar.getModel();
model.addChangeListener(changeListener);
JFrame frame = new JFrame("ScrollBars R Us");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(anotherJScrollBar, BorderLayout.NORTH);
frame.setSize(300, 200);
frame.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);
}
}
}