Java Swing ChangeListener handle JSlider change event

Description

Java Swing ChangeListener handle JSlider change event

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[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL, 10, 100, 20);
    framesPerSecond.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider) e.getSource();
        if (!source.getValueIsAdjusting()) {
          int fps = (int) source.getValue();
          System.out.println(fps);
        }//from  ww w.ja va  2  s.  co m
      }
    });

    frame.add(framesPerSecond,"North");

    frame.setSize(300, 200);
    frame.setVisible(true);
  }

}



PreviousNext

Related