Johnny Dahdah Johnny Dahdah - 1 year ago 163
Java Question

How to get int value from spinner

I'm using NetBeans 7.1 to code in Java. I have a

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.

Answer Source

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 {
} catch ( java.text.ParseException e ) { .. }
int value = (Integer) spinner.getValue();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download