Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The javax.swing package includes JSpinner. A single line of input is contained in JSpinner, which can be a number or an item from an ordered sequence. The user can manually insert legal data into the spinner's text area because there is no need for a drop-down list. When a spinner is pressed, it has an upward and downward arrow to show the previous and next elements.
Constructors of JSpinner
JSpinner(): Creates an empty spinner with no limitations and a zero initial value.
JSpinner(SpinnerModel model): This method creates a spinner using the supplied spinner model as a parameter.
SpinnerListModel(List l): generates a spinner model from list l's items. This spinner model can be used to create a spinning model.
SpinnerNumberModel(int value, int max_value, int min_value, int step): yields a spinner model with a specified step value, a minimum, and maximum value, and a beginning value of value.
setValue(Object v): sets the spinner's value to the object supplied as an 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.
Example of JSpinner
In the following example, we will be creating a simple desktop application using JSpinner that will help you to perform different operations between two numbers. Operations like addition, subtraction, multiplication, division, and, or, xor.
Code
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
class spinner1 extends JFrame implements ChangeListener {
// frame
static JFrame frame;
// label
static JLabel mainLabel, addition, subtraction, multiplication, division, and, or, xor;
// spinner
static JSpinner number1, number2;
// default constructor
spinner1()
{
}
// main class
public static void main(String[] args)
{
// create an object of the class
spinner1 sp1 = new spinner1();
// create a new frame
frame = new JFrame("spinner");
// create a label
mainLabel = new JLabel("Simple Operation Between Two Numbers");
// setting bounds for label
mainLabel.setBounds(260, 10, 300, 20);
addition = new JLabel("0");
addition.setBounds(260, 200, 300, 20);
subtraction = new JLabel("0");
subtraction.setBounds(260, 240, 300, 20);
multiplication = new JLabel("0");
multiplication.setBounds(260, 280, 300, 20);
division = new JLabel("0");
division.setBounds(260, 320, 300, 20);
and = new JLabel("0");
and.setBounds(260, 360, 300, 20);
or = new JLabel("0");
or.setBounds(260, 400, 300, 20);
xor = new JLabel("0");
xor.setBounds(260, 440, 300, 20);
// create a JSpinner with a minimum, maximum and step value
number1 = new JSpinner(new SpinnerNumberModel(20, 1, 100000, 1));
number2 = new JSpinner(new SpinnerNumberModel(20, 1, 100000, 1));
// add change listener to spinner
number1.addChangeListener(sp1);
number2.addChangeListener(sp1);
// set Bounds for spinner
number1.setBounds(330, 70, 50, 50);
number2.setBounds(430, 70, 50, 50);
// set layout for frame
frame.setLayout(null);
// add label
frame.add(mainLabel);
frame.add(addition);
frame.add(subtraction);
frame.add(multiplication);
frame.add(division);
frame.add(and);
frame.add(or);
frame.add(xor);
frame.add(number1);
frame.add(number2);
// set frame size
frame.setSize(800, 600);
frame.show();
}
// if the state is changed
public void stateChanged(ChangeEvent e)
{
int x = (int)number1.getValue();
int y = (int)number2.getValue();
addition.setText("The ADDITION of two numbers is: "+(x+y));
subtraction.setText("The SUBTRACTION of two numbers is: "+(x-y));
multiplication.setText("The MULTIPLICATION of two numbers is: "+(x*y));
division.setText("The DIVISION of two numbers is: "+(((float)x*1.0)/((float)y*1.0)));
and.setText("The AND of two numbers is: "+(x&y));
or.setText("The OR of two numbers is: "+(x|y));
xor.setText("The XOR of two numbers is: "+(x^y));
}
}
You can also try this code with Online Java Compiler
The JSpinner component is part of the javax swing package. A single line of input is contained in JSpinner, which can be a number or an item from an ordered sequence. The user can manually insert legal data into the spinner's text area.
What are the different uses of JSpinner?
Gets or sets the editor for the spinner, which is usually a JSpinner object. The JSpinner constructors use this method to create the spinner's editor.
What is JScrollPane?
A scrollable view of a component is provided by a JScrollPane. Use a scroll pane to display a component that is huge or whose size can change dynamically when screen property is limited. Split panes and tabbed panes are two other containers for saving screen space.
What is setBounds in Java Swing?
setBounds is used to define a component's bounding rectangle. This includes its size and location. The is utilised in several places across the framework. Layout managers utilise it to specify the location and size of a component within its parent container.
What is Jslider in Java?
JSlider is a slider implementation. The user can select a value by sliding the knob inside the bounded value of the component.
Conclusion
In this article, we have discussed the use of JSpinner in Java. Also, we have seen the practical implementation of the JSpinner.