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.
JSpinner contains two constructors which work as follows:
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:
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.