Updated March 28, 2023
Introduction to JSpinner
JSpinner is a component that allows us to select an input that can be an object or a number value from an ordered sequence from an input field. JSpinner is a class, and it is available in the javax.swing package. Like its name suggests, it spins its value in the upward and downward direction; it does not contain a drop-down list to select an input. It basically contains upward and downward arrows. When we try pressing the arrows it shows us the input according to our requirement.
Constructors of JSpinner
JSpinner contains two constructors which work as follows:
- JSpineer(): It makes an empty spinner with the default value set to zero. It is a kind of default constructor with no argument in it.
- JSpineer(SpineerModel model): It takes SprineerModel as an argument that decides its sequence value.
Syntax of Jspineer Class
public class JSpinner extends JComponent
implements Accessible
JSpinner class extends the JComponent Class and implements the Accessible Interface. Here JComponent is the superclass for all the swing components. Any class whose name starts with ‘J’ is the child class for Jcomponent. Jcomponent further extends the Container class. This class provides us the support to add components to the container. Now accessible interface provides us with various classes and interfaces which is used to define a contract between assistive technology and accessible application. It returns the object of AccessibleContext object which turns to provide information about other components.
JSpineer Methods
Different methods available in JSpineer:
- SpineerNumberModel(int value, int max, int min, int step): This method takes four arguments. Spinner initial value would be a value set in the ‘value’ parameter. Now we can specify a maximum value for a spineer i.e. ‘max’ parameter, also we can define a minimum value i.e. ‘min’ parameter and the last parameter is ‘step’ which is responsible to increment or decrement of spineer value with a specified difference.
- SpineerListModel(List I): It creates a spineer model object with the element of List. It requires only one argument to be passed.
- getPreviousValue(): This method we can say acts as a getter for this class to get the previous value of the spinner.
- getnextValue(): This method act as a getter to get the next value of spineer.
- setValue(Object v): This method act as a setter method to set the value of an object passed as an argument.
- getValue(): This is also a getter method that returns the current value of spineer.
Examples of Jspineer
Let us now have a look at some of the examples.
Example #1
In this example, we will create a basic JSpineer for beginners. We will create a simple java class that will extend the JFrame inside it we need to create the object of JFrame, JSpineer both these classes are available in javax.swing package. We can set the bounds for the spinner by calling the setBounds() method which takes four arguments. Then we need to define the layout for our frame by calling setLayout(). At last, we need to add our JSpinner component to the frame object and lastly, we will define the size of our frame by calling setSize() which will take two arguments.
Below you can find the JSpinner example.
import javax.swing.JFrame;
import javax.swing.JSpinner;
public class SpineerDemo extends JFrame {
// frame
static JFrame frame;
// default constructor
SpineerDemo()
{
}
// main class
public static void main(String[] args)
{
// create a new frame
frame = new JFrame("spinner");
// create a JSpinner
JSpinner spineer = new JSpinner();
// set Bounds for spinner
spineer.setBounds(100, 100, 50, 40);
// set layout for frame
frame.setLayout(null);
// add panel to frame
frame.add(spineer);
// set frame size
frame.setSize(400, 400);
frame.show();
}
}
Just run it as a java application a new window will popup which will contain your Jspineer inside the frame. Below find the output attached.
Now we can press the arrow to change the value of the spinner.
Example #2
Now we will see one more example to choose the date of birth from the Jspineer.
package com.cont.article;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class DateOfBirthSpinner extends JFrame implements ChangeListener {
// frame
static JFrame frame;
// define label
static JLabel label, label1;
// define spinner
static JSpinner spineer, spineer1, spineer2;
// default constructor for class
DateOfBirthSpinner()
{
}
// main class
public static void main(String[] args)
{
// createing an object of the class
DateOfBirthSpinner sp1 = new DateOfBirthSpinner();
// create a new frame
frame = new JFrame("spinner");
// create a label
label = new JLabel("select your date of birth");
label1 = new JLabel("1 October 2010");
// create a JSpinner with a minimum, maximum and step value
spineer = new JSpinner();
spineer1 = new JSpinner(new SpinnerNumberModel(1, 1, 31, 1));
// setvalue of year initializing year
spineer.setValue(2000);
// store the months create array
String months[] = { "January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "Novemeber", "December" };
// create a JSpinner with list of values
spineer2 = new JSpinner(new SpinnerListModel(months));
// adding change listener to spinner
spineer.addChangeListener(sp1);
spineer1.addChangeListener(sp1);
spineer2.addChangeListener(sp1);
// set Bounds for all three spinner
spineer.setBounds(70, 70, 50, 40);
spineer1.setBounds(70, 130, 50, 40);
spineer2.setBounds(70, 200, 90, 40);
// setbounds for all label
label.setBounds(10, 10, 150, 20);
label1.setBounds(10, 300, 150, 20);
// set layout for frame
frame.setLayout(null);
// add label to frame object
frame.add(label);
frame.add(label1);
frame.add(spineer);
frame.add(spineer1);
frame.add(spineer2);
// add panel to frame object
frame.add(spineer);
// set frame size
frame.setSize(300, 300);
frame.show();
}
// if the state is changed for spinner set value to label
public void stateChanged(ChangeEvent e)
{
label1.setText(spineer1.getValue() + " " + spineer2.getValue() + " " + spineer.getValue());
}
}
Below you can find the output of the above program.
Here we are defining three spinners for the year, month and date. So we are going to define three spinners here.
Conclusion – JSpinner
JSpinner can be used where number or object type input is required in a sequence or in order. Which can be either increment or decrement and the order of value wan to persist. In that scenario, we can go for JSpineer.
Recommended Articles
This is a guide to JSpinner. Here we discuss constructors, syntax, and methods of JSpinner along with different examples and code implementation. You may also look at the following articles to learn more –