How to get int value from spinner

99封情书 提交于 2019-11-30 11:33:28
Adeel Ansari

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();
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.

    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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!