How to cancel a potentially endless loop via clicked button?
<p>I'm trying to realize a certain pattern for a little DigitalWatch class with minimal interface in java (just a practice).</p>

<p>It has 2 buttons: a mode button and an increment button.</p>

<p>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). </p>

<p>My method <code>displayTime()</code> is something like this:</p>

<pre><code>void displayTime()
{
    while (pressed==false)
    {
        try
        {
            display.setText(String.format("%02d:%02d", hours, minutes));
            display.repaint();
            Thread.sleep(1000);
            minutes = minutes + 1;
            if (minutes &gt; 59)
            {
                minutes = 0;
                hours = hours + 1;
            }
            if (hours &gt; 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();
}
</code></pre>

<p>But it seems that while this loop is active the button looses its clickability.</p>

<p>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?</p>