AddingChangeListenerToJSpinner.java Source code

Java tutorial

Introduction

Here is the source code for AddingChangeListenerToJSpinner.java

Source

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

public class AddingChangeListenerToJSpinner {

    public static void main(String[] a) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JSpinner dateSpinner = new JSpinner();

        dateSpinner.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                SpinnerModel dateModel = dateSpinner.getModel();
                System.out.println(dateModel.getValue());
            }
        });
        frame.add(dateSpinner, "North");

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

}