JSlider: addChangeListener(ChangeListener l) : JSlider « javax.swing « Java by API






JSlider: addChangeListener(ChangeListener l)

 

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    final JSlider slider = new JSlider(0, 150, 0);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    slider.setPreferredSize(new Dimension(150, 30));
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent event) {
        int value = slider.getValue();
        if (value == 0) {
          System.out.println("0");
        } else if (value > 0 && value <= 30) {
          System.out.println("value > 0 && value <= 30");
        } else if (value > 30 && value < 80) {
          System.out.println("value > 30 && value < 80");
        } else {
          System.out.println("max");
        }
      }
    });
    f.add(slider);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

   
  








Related examples in the same category

1.JSlider.isFilled
2.Slider.horizontalThumbIcon
3.new JSlider(int min, int max)
4.new JSlider(int min, int max, int value)
5.new JSlider(int orientation)
6.new JSlider(int orientation, int min, int max, int value)
7.JSlider: getExtent()
8.JSlider: getMaximum()
9.JSlider: getMinimum()
10.JSlider: getPaintTicks()
11.JSlider: getSnapToTicks()
12.JSlider: getValue()
13.JSlider: getValueIsAdjusting()
14.JSlider: setExtent(int extent)
15.JSlider: setInverted(boolean b)
16.JSlider: setLabelTable(Dictionary labels)
17.JSlider: setMajorTickSpacing(int v)
18.JSlider: setMaximum(int maximum)
19.JSlider: setMinimum(int minimum)
20.JSlider: setMinorTickSpacing(int v)
21.JSlider: setOrientation(int orientation)
22.JSlider: setPaintLabels(boolean t)
23.JSlider: setPaintTicks(boolean t)
24.JSlider: setPaintTrack(boolean b)
25.JSlider: setPreferredSize(Dimension preferredSize)
26.JSlider: setSnapToTicks(boolean b)
27.JSlider: setToolTipText(String s)
28.JSlider: setValue(int n)
29.extends JSlider