Bounded Range Model with JSlider : BoundedRangeModel « Swing « Java Tutorial






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);
  }
}








14.31.BoundedRangeModel
14.31.1.Bounded Range Model with JSlider
14.31.2.Using BoundedRangeModelUsing BoundedRangeModel