I'm trying to realize a certain pattern for a little DigitalWatch class with minimal interface in java (just a practice).
It has 2 buttons: a mode button and an increment button.
The mode button has a listener which triggers the change of my program's state between "SetHours,SetMinutes,DisplayTime" (which exist as objects due to the pattern, the objects call the specific state-dependend methods in my DigitalWatch class).
My method displayTime()
is something like this:
void displayTime()
{
while (pressed==false)
{
try
{
display.setText(String.format("%02d:%02d", hours, minutes));
display.repaint();
Thread.sleep(1000);
minutes = minutes + 1;
if (minutes > 59)
{
minutes = 0;
hours = hours + 1;
}
if (hours > 23)
{
hours = 0;
}
display.setText(String.format("%02d:%02d", hours, minutes));
display.repaint();
} catch (Exception e)
{
break;
}
}
display.setText(String.format("%02d:%02d", hours, minutes));
display.repaint();
}
But it seems that while this loop is active the button looses its clickability.
So once the "time counts" i am stuck with this state and am no longer able to change the mode. Is there a good way/best practice to maintain the clickability of my modebutton?