nhix
nhix

Reputation: 81

Get the exact jSpinner Value

Can somebody tell me how can I get the exact value from jSpinner? Here is how my jSpinner's appear when user will select time: enter image description here

however, when I pass the jSpinner value to textfield it appears like this: enter image description here

Here is my code:

timeout1 = spnrTOH1.getValue()+":"+spnrTOM1.getValue()+" "+spnrTOA1.getValue();

Here is my jSpinner model: enter image description here

Any help would be much appreciated. Thanks in advance! Sorry if my question seems silly. Newbie here. :)

Upvotes: 0

Views: 757

Answers (2)

ELITE
ELITE

Reputation: 5940

add this function to your program..

public String getString(Object object) {
    int number = Integer.parseInt(object.toString().trim());
    if(number < 10) {
        return "0" + number;
    }
    return String.valueOf(number);
}

and call this when you have to set the value to text field like

timeout1 = getString(spnrTOH1.getValue())+":"+getString(spnrTOM1.getValue())+" "+spnrTOA1.getValue();

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32517

It is not related to spinner nor its model.

You need to format your string to have leading zeros. The spinner is showing 00 when the exact value is 0 due to spiner's renderer formatting procedure. Renderer is a component that is responsible for showing on GUI value held by the model. Eg. Custom renderer can display integers as Roman numbers.

To format your output simply use String#format method like that:

timeout1 = String.format("%02d:%02d %s",spnrTOH1.getValue(),spnrTOM1.getValue(),spnrTOA1.getValue());

This way you will display integer values as 2 digits numbers with leading 0.

Upvotes: 2

Related Questions