import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ChangeSlider {
public static void main(String args[]) {
JFrame f = new JFrame("Change Slider");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSlider aJSlider = new JSlider(JSlider.HORIZONTAL);
ChangeListener aChangeListener = new BoundedChangeListener();
BoundedRangeModel model = aJSlider.getModel();
model.addChangeListener(aChangeListener);
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);
}
}
}
|