How to create a Number Spinner in Java?

Java 8Object Oriented ProgrammingProgramming

At first, create a SpinnerModel −

SpinnerModel value = new SpinnerNumberModel(50, 0, 75, 1);

Now, set the values −

JSpinner spinner = new JSpinner(value);

The following is an example to create a number spinner −

Example

package my;
import javax.swing.*;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame("Spinner Demo");
      SpinnerModel value = new SpinnerNumberModel(50, 0, 75, 1);
      JSpinner spinner = new JSpinner(value);
      spinner.setBounds(50, 80, 70, 100);
      frame.add(spinner);
      frame.setSize(550,300);
      frame.setLayout(null);
      frame.setVisible(true);
   }
}

This will produce the following output −

raja
Published on 03-Jun-2019 11:25:51
Advertisements