Z. Reticulan
Z. Reticulan

Reputation: 155

How to get JSpinner min and max values in order to set a random value

I have a function that will use JSpinner.setValue() to assign a random value within the spinner's bounds. Of course, I need to know the spinner's minimum and maximum values, which are already assigned. At this point, I'm cycling through the entire spinner to determine these values. Is there a better way to find them? Something like

JSpinner.getModel().getMaxValue();

I have already read through similar questions including this one: JSpinner get Max Min Value, but it didn't answer my question. Any help is appreciated.

Upvotes: 1

Views: 3495

Answers (2)

Infinyte
Infinyte

Reputation: 51

Here is an example of getting the min/max value from a JSpinner where the min/max values have been set as Double values.

// Get Minimum and Maximum values from jSpinner's Model 
    double max = (Double) ((SpinnerNumberModel)    jSpinner.getModel()).getMaximum();//MAX_ALLOWED
    double min = (Double) ((SpinnerNumberModel) jSpinner.getModel()).getMinimum();//LOWEST_ALLOWED

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32517

I assume that you are talking about numerical values. In such case you should use NumberSpinnerModel - this class already has method you requested: getMaximum() and getMinimum().

JSpinner.getModel() returns SpinnerModel so you will have to cast it to SpinnerNumberModel. This is ofcourse if you are really using this model with your spinner - cast will fail otherwise.

Upvotes: 4

Related Questions