Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get int value from spinner

I'm using NetBeans 7.1 to code in Java. I have a JFrame where I have spinner with integer values on it, I want to know how to get the active value in the spinner, I mean, the one that the user picks when the program is running; to use it on another methods.

like image 639
Johnny Dahdah Avatar asked Mar 14 '13 03:03

Johnny Dahdah


People also ask

How to get the value from a spinner in Java?

int value = (Integer) spinner. getValue();

What is JSpinner in Java?

JSpinner is a part of javax. swing package. JSpinner contains a single line of input which might be a number or a object from an ordered sequence. The user can manually type in a legal data into the text field of the spinner. The spinner is sometimes preferred because they do not need a drop down list.


1 Answers

spinner.getValue() should do the trick. You can cast it to Integer, like

int value = (Integer) spinner.getValue();

Note from reggoodwin: You should also call spinner.commitEdit() prior to calling getValue() to ensure manually typed values with the editor are propagated to the model, otherwise you will only get the old value.

Hence, it should be something like below,

try {
    spinner.commitEdit();
} catch ( java.text.ParseException e ) { .. }
int value = (Integer) spinner.getValue();
like image 132
Adeel Ansari Avatar answered Oct 22 '22 12:10

Adeel Ansari