Java Swing | JSpinner
JSpinner is a part of javax.swing package. JSpinner contains a single line of input which might be a number or a object from an 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 contain an 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 an initial value set to zero and no constraints.
- JSpinner( SpinnerModel model): Creates a spinner with a specified spinner model passed as an 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
2. Program to create a JSpinner and add ChangeListener to it; Program to select your date of birth using JSpinner.
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.