This site requires JavaScript, please enable it in your browser!
Greenfoot back
Nosson1459
Nosson1459 wrote ...

2017/2/12

JSpinner

Nosson1459 Nosson1459

2017/2/12

#
I am making a timer program that almost works... I just don't know how to display the time in the way that I want. I want that when the start button gets pressed the hour and minute JSpinner both get replaced with one big JLabel and then switches back when cancel is pressed? Also, is there a way to make the JSpinners loop through the numbers they contain and that you can't type in a number but you have to use the arrows?
danpost danpost

2017/2/12

#
You should be able to toggle the text of the start button and alternate what it does -- which should just be a matter of changing the visibility of the components using 'setVisible' on them. I am guessing that you could use the 'setEditor' method of the JSpinner class to change the editor component to one of type JLabel. Then have any changes in value of the JSpinner update the text of the label by using 'setText' on it.
Nosson1459 Nosson1459

2017/2/13

#
danpost wrote...
You should be able to toggle the text of the start button and alternate what it does
I know how to choose what the button does the problem is placing one big JLabel in place of the two JSpinners (and the other two unmentioned JLabels).
-- which should just be a matter of changing the visibility of the components using 'setVisible' on them.
I guess this is the answer.
I am guessing that you could use the 'setEditor' method of the JSpinner class to change the editor component to one of type JLabel. Then have any changes in value of the JSpinner update the text of the label by using 'setText' on it.
I don't want a label spinner I want no spinner like above. But, there were two other questions. This maybe can have answered the making the text box not editable but I just ended up doing:
1
((JSpinner.DefaultEditor) hourSpinner.getEditor()).getTextField().setEditable(false);
because I wanted it to stay as a text box. As for the looping, I will make the minute editor be able to go to 60 instead of 59 and start at -1 and add a listener to see when it's changed, if it is changed to 60 then it will be set straight to 0 or 1 depending on what the hourSpinner is (0 or >0) if it's changed to -1 then I will change it to 59. Is there an easier way to do this, I will also be running into problems because I can't have both spinners set to 0 if the hour is zero and you want to pass by zero to get to 59 I will go straight to 1?
Nosson1459 Nosson1459

2017/2/13

#
So from the original question(s) the only one left is the looping question (make sure to see the previous post about it).
danpost danpost

2017/2/13

#
I do not see any issues in having the value range from -1 to 60. The checks will be on -1 and 60 and adjust the value to 59 and 0, respectively. There is no conflict there.
Nosson1459 Nosson1459

2017/2/13

#
With doing that I run in to a separate problem which is that I have coding which makes it that when the hour spinner && the minute spinner are both 0 it automatically sets the minute spinner to 1, so the only problem is when the hour is == 0 and i'm pressing the down arrow. I thought of a way to fix it but I didn't try it yet. I can make a variable with class level scope and it will remember the last number the spinner was on so if the variable lastMinuteValue == 59 (or 60 depending how my code works out) && minuteValue == 0 && hourValue == 0 then I will set the spinner to 59 but if (lastMinuteValue == 1 && minuteValue == 0 && hourValue == 0) then I will set the spinner to 59. I hope this works.
Nosson1459 Nosson1459

2017/2/13

#
I didn't need the lastMinuteValue variable I just did 'if (minuteValue == 60 && hourValue != 0)' here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void spinnerStateChanged(ChangeEvent e)
{
    int hourValue = Integer.valueOf(hourSpinner.getValue().toString()).intValue();
    int minuteValue = Integer.valueOf(minuteSpinner.getValue().toString()).intValue();
    if (minuteValue == 60 && hourValue != 0)
    {
        minuteSpinner.setValue(0);
    }
    else if (minuteValue == -1)
    {
        minuteSpinner.setValue(59);
    }
    if (hourValue == 0 && minuteValue == 0)
    {
        minuteSpinner.setValue(59);
    }
    else if (hourValue == 0 && minuteValue == 60)
    {
        minuteSpinner.setValue(1);
    }
}
Thanks for the help.
You need to login to post a reply.