Java JSpinner handle change event

Description

Java JSpinner handle change event

import java.awt.FlowLayout;

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

public class Main extends JFrame {
   public Main() {
      super("java2s.com");

      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setLayout(new FlowLayout());

      JSpinner spinner = new JSpinner();
      spinner.addChangeListener(new SpinnerListener());

      spinner.setValue(new Integer(100));

      getContentPane().add(spinner);/*from w w  w .  ja  v  a2s .  co  m*/
   }

   public static void main(String[] args) {
      Main frame = new Main();
      frame.pack();
      frame.setVisible(true);
   }
}

class SpinnerListener implements ChangeListener {
   public void stateChanged(ChangeEvent evt) {
      JSpinner spinner = (JSpinner) evt.getSource();

      // Get the new value
      Object value = spinner.getValue();
   }
}



PreviousNext

Related