I have a mulitpanel frame that serves different display purposes, one of them is used to show a time countdown string which excuted by a scheduled timetask:
private class TimerPad extends JPanel {
private int timeLeft = 60;
private String prefix = "Time Left: ";
TimerPad() {
setDoubleBuffered(true);
}
public void start() {
new java.util.Timer().schedule(new TimerTask() {
@Override
public void run() {
if (gameStatus == BoardStatus.JUDGING) {
this.cancel();
} else if (timeLeft == 0) {
submitCards(cardsSouth);
this.cancel();
} else {
timeLeft--;
repaint();
}
}
}, 1000, 1000);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
.....
}
@Override
public Dimension getPreferredSize() {
return new Dimension(240, 50);
}
}
This panel is added in the main frame, and its start() method triggered when it externally requested. Now the timePad only seems to be faring fine if the normal 60 secs elapsed, but when I use a button(i.e. submit) try to cancel the task earlier all the GUI are frozen ... I tried to wrap a runner for the task but it turned out the same...