JSpinner is a part of javax.swing package . JSpinner contains a single line of input which might be a number or a object from a ordered sequence . The user can manually type in a legal data into the text field of the spinner . The spinner is sometimes preferred because they do not need a drop down list . Spinners contains a upward and a downward arrow to show the previous and the next element when it is pressed.
Constructors of JSpinner are:
- JSpinner() : Creates an empty spinner with initial value set to zero and no contraints
- JSpinner( SpinnerModel model)
creates a spinner with a specified spinner model passed as argument .Commonly used methods are :
- SpinnerListModel(List l) : creates a spinner model with elements of list l . This spinner model can be used to set as a model for spinner.
- SpinnerNumberModel(int value, int max, int min, int step) : returns a spinner model whose initial value is set to value, with minimum and maximum value, and a definite step value.
- setValue(Object v) : sets the value of the spinner to the object passed as argument.
- getValue() : returns the current value of the spinner.
- getPreviousValue() : returns the previous value of the spinner.
- getNextValue() : returns the next value of the spinner.
1. Program to create a simple JSpinner
// java Program to create a// simple JSpinnerimportjava.awt.event.*;importjavax.swing.*;importjava.awt.*;classspinnerextendsJFrame {// framestaticJFrame f;// default constructorspinner(){}// main classpublicstaticvoidmain(String[] args){// create a new framef =newJFrame("spinner");// create a JSpinnerJSpinner s =newJSpinner();// set Bounds for spinners.setBounds(70,70,50,40);// set layout for framef.setLayout(null);// add panel to framef.add(s);// set frame sizef.setSize(300,300);f.show();}}Output :

2. Program to create a JSpinner and add ChangeListener to it ; Program to select your date of birth using JSpinner)
// Java program to select your// date of birth using JSpinnerimportjava.awt.event.*;importjavax.swing.*;importjava.awt.*;importjavax.swing.event.*;classspinner1extendsJFrameimplementsChangeListener {// framestaticJFrame f;// labelstaticJLabel l, l1;// spinnerstaticJSpinner s, s1, s2;// default constructorspinner1(){}// main classpublicstaticvoidmain(String[] args){// create an object of the classspinner1 sp1 =newspinner1();// create a new framef =newJFrame("spinner");// create a labell =newJLabel("select your date of birth");l1 =newJLabel("1 January 2000");// create a JSpinner with a minimum, maximum and step values =newJSpinner();s1 =newJSpinner(newSpinnerNumberModel(1,1,31,1));// setvalue of years.setValue(2000);// store the monthsString months[] = {"January","February","March","April","May","June","July","August","September","October","Novemeber","December"};// create a JSpinner with list valuess2 =newJSpinner(newSpinnerListModel(months));// add change listener to spinners.addChangeListener(sp1);s1.addChangeListener(sp1);s2.addChangeListener(sp1);// set Bounds for spinners.setBounds(70,70,50,40);s1.setBounds(70,130,50,40);s2.setBounds(70,200,90,40);// setbounds for labell.setBounds(10,10,150,20);l1.setBounds(10,300,150,20);// set layout for framef.setLayout(null);// add labelf.add(l);f.add(l1);f.add(s);f.add(s1);f.add(s2);// add panel to framef.add(s);// set frame sizef.setSize(400,400);f.show();}// if the state is changedpublicvoidstateChanged(ChangeEvent e){l1.setText(s1.getValue() +" "+ s2.getValue() +" "+ s.getValue());}}Output :

Note : This program will not run in an online compiler please use an Offline IDE
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


