Possible Duplicate:
GUI threading in java
I have been trying to make a text based game and it is going so far, except for this strange bug when using Thread.sleep() and wait() This code should print a message char by char to a JTextArea called console with a delay between each one.
Here is the code with wait()
int i=0;
synchronized(mon) {
while(i<msg.length())
{
console.setText(console.getText()+ msg.charAt(i));
i++;
try {
mon.wait(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Here is the code with sleep():
int i=0;
while(i<msg.length())
{
console.setText(console.getText()+ msg.charAt(i));
i++;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
However, when it reaches this code, the program waits msg.length*500ms then prints the whole msg instantly! Help!