In Java, I've been trying to cast a JSpinner number input to an integer using Netbeans. I've made it so that the JSpinner input is a number, but I can't try to cast it to my own integer without receiving errors. I've tried many various things and one of the errors I most commonly receive is "Inconvertible types, required int, found JSpinner." How would I just simply be able to assign a JSpinner input (preferably a number) to an integer.
Asked
Active
Viewed 6,346 times
1
-
2*"I've tried many various things"* For better help sooner, post an [SSCCE](http://sscce.org/) of the one you think is closest to working. – Andrew Thompson May 04 '13 at 04:11
1 Answers
4
You can't cast the spinner itself to a number. You need to get its value. You can use:
((SpinnerNumberModel) spinner.getModel())).getNumber().intValue()
This will only work if the underlying model is a SpinnerNumberModel
(default). You could also use:
Object o = spinner.getValue();
Number n = (Number) o;
int i = n.intValue();
This will work as long as the value can be cast to a Number
.

wchargin
- 15,589
- 12
- 71
- 110
-
@Mel yes, true; this is example code, not production code. In reality I'd keep a reference to the original model. – wchargin May 04 '13 at 04:15