0

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. :)

nhix
  • 81
  • 3
  • 13
  • i checked the code. working fine.. whats the issue?? on which event you want to display it on textfield.?? – ELITE May 13 '15 at 08:03
  • Hi @NiRRaNjANRauT , I want to have 01:00 AM and not 1:0 AM. I want to display on textfield when the submit button is triggered. thanks – nhix May 13 '15 at 08:05
  • what `model` you had set to `JSpinner`...means `DefaultModel` or any other... – ELITE May 13 '15 at 08:10
  • I've set it as number. I'll edit my question and add the screenshot of the model. – nhix May 13 '15 at 08:12

2 Answers2

2

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.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Hi Thank you for your answer. But the first answer works for me. I appreciate your help. Big Thanks! :) – nhix May 13 '15 at 08:33
1

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();
ELITE
  • 5,815
  • 3
  • 19
  • 29