Udi Oshi Udi Oshi - 1 year ago 63
Java Question

GUI isn't shown while thread is in procces in JAVA

I'm making simple game with very simple thread (1 sec delay)
got problem with the thread, I have

while(true)
loop with the code:

try {
while (true) {
Ltimer.setText(getTimeElapsed());
Thread.currentThread();
Thread.sleep(1000); // Thread sleeping for 1 second
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "error with timer");
}


it simply get string every second and updates label text
when I'm trying to run it the gui freeze and I can only see the label in a black background, all buttons and bg img dissappeared. tried to fix with
setVisible()

repaint()

but got nothing..

any other options?

Answer Source

don' use Thread#sleep(int) during EDT, then you have issue with Concurency in Swing, if you need to delay any action use java.swing.Timer, example for EDT lack here