Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.sleep and repainting

I have a panel that displays text. I want the panel to change its text and then have the application pause before anything else happens. I'm using Thread.sleep(1000). For some reason, though, the application doesn't finish painting the panel before Thread.sleep gets called (the text doesn't get changed). I also tried this:

board.invalidate();
board.setLeftMessage("Not");
board.setRightMessage("Here");
board.revalidate();
Date current = new Date();
long timeNow = current.getTime();
Date newDate = new Date(timeNow + 1000);
while (current.before(newDate))
    current = new Date();

but no luck there either. Anyone have a suggestion? Thanks so much.

like image 444
rach Avatar asked Nov 22 '25 09:11

rach


1 Answers

You are blocking the AWT Event Dispatch Thread (EDT). The EDT handle repainting and input events, so your code need not be multithreaded (which would be effectively impossible). Use javax.swing.Timer to send an event later on the EDT. (Do not confuse javax.swing.Timer with java.util.Timer!)

like image 99
Tom Hawtin - tackline Avatar answered Nov 24 '25 21:11

Tom Hawtin - tackline