- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to limit the values in a Number JSpinner Component with Java?
To limit the values in a number JSpinner component, use the SpinnerNumberModel that allows to set the min, max, step size and even the initial value −
value − current value of the model min − first number in the sequence max − last number in the sequence stepSize − difference between elements of the sequence
Let us set the above values −
int min = 0; int max = 10; int step = 1; int i = 1; SpinnerModel value = new SpinnerNumberModel(i, min, max, step);
Now, we will set these values to our JSpinner −
JSpinner spinner = new JSpinner(value);
The following is an example to limit the values in a number JSpinner component −
Example
package my; import java.awt.GridBagLayout; import javax.swing.*; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Spinner Demo"); JPanel panel = new JPanel(); JLabel label = new JLabel("Rank − "); panel.setLayout(new GridBagLayout()); int min = 0; int max = 10; int step = 1; int i = 1; SpinnerModel value = new SpinnerNumberModel(i, min, max, step); JSpinner spinner = new JSpinner(value); spinner.setBounds(50, 80, 70, 100); panel.add(label); panel.add(spinner); frame.add(panel); frame.setSize(550,300); frame.setVisible(true); } }
This will produce the following output −
- Related Articles
- How to create a JSpinner in Java with values as numbers?
- Limit number of values in a field with MongoDB?
- Limit number of values in a field using MongoDB?
- How to find a node in a JTree Component with Java?
- How to disallow resizing component with GridBagLayout in Java
- Set a component and place it next to the previously added component with GridBagLayout in Java
- How can we limit the number of characters inside a JTextField in Java?
- How to get the leaf after this node in a JTree component with Java?
- How to create a JMenuBar Component in Java?
- How to center align component using BoxLayout with Java?
- How to add a tooltip to a component in Java?
- Python – Limit the values to keys in a Dictionary List
- Set the location of component in a GridBagLayout with Java
- Clip (limit) the values in a Numpy array
- How to select the top two values using LIMIT in MySQL?

Advertisements