Java Swing How to - Set range for Number value JSpinner








Question

We would like to know how to set range for Number value JSpinner.

Answer

//  w  w w . j  a v a2  s  .c  o m
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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

public class Main extends JFrame {
  public Main() {
    JSpinner m_numberSpinner;
    SpinnerNumberModel m_numberSpinnerModel;
    Double current = new Double(5.50);
    Double min = new Double(0.00);
    Double max = new Double(10.00);
    Double step = new Double(0.25);
    m_numberSpinnerModel = new SpinnerNumberModel(current, min, max, step);
    m_numberSpinner = new JSpinner(m_numberSpinnerModel);
    add(m_numberSpinner);
  }
  public static void main(String argv[]) {
    Main spinnerFrame = new Main();
    spinnerFrame.setSize(350, 200);
    spinnerFrame.setVisible(true);
  }
}