Java Tutorial/Swing/BoundedRangeModel
Bounded Range Model with JSlider
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main extends JFrame {
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSlider slider = new JSlider();
BoundedRangeModel model = slider.getModel();
model.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
BoundedRangeModel m = (BoundedRangeModel) e.getSource();
System.out.println("Slider position changed to " + m.getValue());
}
});
getContentPane().add(slider);
pack();
}
public static void main(String[] args) {
Main m = new Main();
m.setVisible(true);
}
}
Using BoundedRangeModel
The interface contains four interrelated properties that are necessary to describe a range of values: minimum, maximum, value, and extent.
The settings for the four properties must abide by the following ordering:
minimum <= value <= value+extent <= maximum