PDA

View Full Version : Java: casting an object to an int


Fumigator
05-18-2006, 05:03 PM
I'm stumped. I'm sure it's because I'm not in the right paradigm, but I'm trying to get there.

I'm writing a Swing app, and I have a JSpinner object that I modeled on the SpinnerNumberModel. There is a getPreviousValue method that returns the value of the previous number in the sequence, only in Object form! I need to work with this number as an int. How can I cast that Object as an int?

Fumigator
05-18-2006, 06:03 PM
I guess I switched my paradigm because I was able to resolve it this way:

new Integer(levelField.getValue().toString()).intValue()

Typical Java Nonsense! :thumbsup:

ghell
05-19-2006, 12:52 PM
Yup thats pretty much it, you could prob miss the toString() in there and it would still work im not sure. To go from "2" to 2 you would generally use Integer.parseInt(String) but the method you have used generally works for objects which are numerical values really but set as objects. It depends how the slider does it :) (might want to look at the source of the api)