Udi Oshi Udi Oshi - 2 months ago 28
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

loop with the code:

try {
while (true) {
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


but got nothing..

any other options?


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