You are reading the article Constructors, Syntax, And Methods Of Jspinner With Examples updated in October 2023 on the website Dacquyenphaidep.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Constructors, Syntax, And Methods Of Jspinner With Examples
Introduction to JSpinnerWeb development, programming languages, Software testing & others
Constructors of JSpinnerJSpinner 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 AccessibleJSpinner 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 MethodsDifferent 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 JspineerLet us now have a look at some of the examples.
Example #1In 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.
import javax.swing.JFrame; import javax.swing.JSpinner; public class SpineerDemo extends JFrame { static JFrame frame; SpineerDemo() { } public static void main(String[] args) { frame = new JFrame("spinner"); JSpinner spineer = new JSpinner(); spineer.setBounds(100, 100, 50, 40); frame.setLayout(null); frame.add(spineer); 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 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 { static JFrame frame; static JLabel label, label1; static JSpinner spineer, spineer1, spineer2; DateOfBirthSpinner() { } public static void main(String[] args) { DateOfBirthSpinner sp1 = new DateOfBirthSpinner(); frame = new JFrame("spinner"); label = new JLabel("select your date of birth"); label1 = new JLabel("1 October 2010"); spineer = new JSpinner(); spineer1 = new JSpinner(new SpinnerNumberModel(1, 1, 31, 1)); spineer.setValue(2000); String months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novemeber", "December" }; spineer2 = new JSpinner(new SpinnerListModel(months)); spineer.addChangeListener(sp1); spineer1.addChangeListener(sp1); spineer2.addChangeListener(sp1); spineer.setBounds(70, 70, 50, 40); spineer1.setBounds(70, 130, 50, 40); spineer2.setBounds(70, 200, 90, 40); label.setBounds(10, 10, 150, 20); label1.setBounds(10, 300, 150, 20); frame.setLayout(null); frame.add(label); frame.add(label1); frame.add(spineer); frame.add(spineer1); frame.add(spineer2); frame.add(spineer); frame.setSize(300, 300); frame.show(); } 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 – JSpinnerJSpinner 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 ArticlesThis 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 –
You're reading Constructors, Syntax, And Methods Of Jspinner With Examples
Update the detailed information about Constructors, Syntax, And Methods Of Jspinner With Examples on the Dacquyenphaidep.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!