How to get/store value from JSpinner?

545 Views Asked by At

Given the following code, how do I store the selected value from the spinner in thickness1?

JSpinner thickn=new JSpinner();
thickn = new JSpinner(new SpinnerNumberModel(1, 1, 60, 1));      
thickn.setFont(new Font("Arial Sans-seriff", Font.BOLD, 12));
thickn.setBounds(120,105,100,25); 
// ...
int thickness1 = (Integer) thickn.getValue();

Is there an code I can add at the 3 dot region to retrieve the value from the spinner?

1

There are 1 best solutions below

2
Kirit On

In your last line, try

int thickness1 = ((SpinnerNumberModel)thickn.getModel()).getNumber().intValue();

If you want to be more explicit, you can add the following where you have the three dots, it will do the same thing as the line above

SpinnerModel model = thickn.getModel();
SpinnerNumberModel numberModel = (SpinnerNumberModel)model;
Number number = numberModel.getNumber();
int thinkness1 = number.intValue();