Java SpinnerNumberModel create

Introduction

We can create SpinnerNumberModel as follows:

public SpinnerNumberModel()
SpinnerModel model = new SpinnerNumberModel();
JSpinner spinner = new JSpinner(model);

public SpinnerNumberModel(double value, double minimum, double maximum, double stepSize)
SpinnerModel model = new SpinnerNumberModel(50, 0, 100, .25);
JSpinner spinner = new JSpinner(model);

public SpinnerNumberModel(int value, int minimum, int maximum, int stepSize)
SpinnerModel model = new SpinnerNumberModel(50, 0, 100, 1);
JSpinner spinner = new JSpinner(model);

public SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize)
Number value = new Integer(50);
Number min = new Integer(0);
Number max = new Integer(100);
Number step = new Integer(1);
SpinnerModel model = new SpinnerNumberModel(value, min, max, step);
JSpinner spinner = new JSpinner(model);
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class Main extends JFrame {
   public Main() {
      super("java2s.com");

      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setLayout(new FlowLayout());

      int minValue = 1;
      int maxValue = 10;
      int currentValue = 5;
      int steps = 1;
      SpinnerNumberModel nModel = new SpinnerNumberModel(currentValue, minValue, maxValue, steps);
      JSpinner spinner = new JSpinner(nModel);

      getContentPane().add(spinner);//from w  ww . ja  va2 s.  co m
   }

   public static void main(String[] args) {
      Main frame = new Main();
      frame.pack();
      frame.setVisible(true);
   }
}



PreviousNext

Related