How to get int value from spinner
Asked Answered
D

3

19

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.

Disregard answered 14/3, 2013 at 3:40 Comment(3)
Please don't add the language in title since the question is already tagged with it.Verticillaster
@LuiggiMendoza Sorry, I didn't know that.Disregard
Apart from that, what have you tried?Verticillaster
J
36

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();
Janettjanetta answered 14/3, 2013 at 3:44 Comment(1)
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. There is an example in the JSpinner class javadocs.Biopsy
R
2
String value = getSpinner().getValue() + "";

Integer.parseInt(value)

My solution, this working for me...
Not work:

Integer.parseInt( getSpinner().getValue().toString()) //get object toString

I do not understand, but it works, I leave it in case anyone needs it.

Reneta answered 14/8, 2017 at 6:57 Comment(0)
G
0
    String spinner = "catch Value";
    Integer myint = (Integer) jSpinner1.getValue();
    spinner = myint.toString();
    jTextField1.setText(spinner);

This worked for me. Wanted to write the Integer value from jSpinner to a textfield.

Gallinaceous answered 20/6, 2017 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.