Slider ChangeListener : Slider « Swing JFC « Java

Slider ChangeListener

Slider ChangeListener
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");
    JSlider aJSlider = new JSlider(JSlider.HORIZONTAL);
    ChangeListener aChangeListener = new BoundedChangeListener();
    BoundedRangeModel model = aJSlider.getModel();
    Container c = f.getContentPane();
    c.add(aJSlider, BorderLayout.SOUTH);
    f.setSize(300, 200);

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


Related examples in the same category

1.Creating a JSlider Component
2.Create a horizontal slider with custom min, max, and value
3.Create a vertical slider with min=0, max=100, value=50
4.Create a vertical slider with custom min, max, and value
5.Make the horizontal slider move right-to-left
6.Make it vertical and move bottom-to-top
7.Make it vertical and move top-to-bottom
8.Get the extent
9.Getting and Setting the Values of a JSlider Component
10.Drawing with Swing, using a JSliderDrawing with Swing, using a JSlider
11.Using progress bars and slidersUsing progress bars and sliders
12.A slider with tick marks and labels
13.An example of JSlider with default labelsAn example of JSlider with default labels
14.Set minor tick marks every 5 units
15.Set major tick marks every 25 units
16.Slider SampleSlider Sample
17.JSlider Sample 2JSlider Sample 2
18.Inverted SlidersInverted Sliders
19.Slider change action listenerSlider change action listener
20.Date sliderDate slider
21.Tick SlidersTick Sliders
22.Scroll SliderScroll Slider
23.Text SliderText Slider
24.Change the minimum value
25.Change the maximum value
26.Set the extent
27.Move the slider by a fixed amount: the extent.
28.Set all the values at once by using the model
29.Set the value; the new value will be forced into the bar's range
30.Listening for Value Changes in a JSlider Component
31.Constraining JSlider Component Values to Tick Marks
32.Determine if currently snapping to tick marks
33.Snap to tick marks
34.Set to a spot between tick marks; the value moves to closest tick mark
35.Determine if currently painting labels
36.Showing Tick Marks on a JSlider Component
37.Show tick marks
38.The slider allows you to use an arbitrary label at any particular major tick mark.
39.Showing Tick-Mark Labels on a JSlider Component
40.Hide the track
41.Sample SlidersSample Sliders
42.A frame with many sliders and a text field to show slider valuesA frame with many sliders and a text field to show slider values