I am currently writing a really simple program using JSwing. There is a JTextArea ("textArea")
inside a JScrollPane ("textPane")
. I managed to edit texts and stuff in this TextArea
with a StyledDocument
named doc. However, when I want to insert a string into this document in a while loop, all texts appear at once after the loop ends. The effect I want is to see the texts come up one by one and line by line with the help of Thread.sleep()
.
Here is my code sample:
while (listening == false && a <= StoryInterface.getDiaNum()) {
doStoryMode(a, b);
Thread.sleep(100);
if (b == StoryInterface.getNumOfSentence()[a] - 1) {
b = 1;
a ++;
} else {
b ++;
}
}
Where doStoryMode(a, b)
is a simple method that calls doc.insertString(...)
. The program is working, but I couldn't see things coming up one by one. I tried to solve this problem by writing textPane.repaint()
and textArea.repaint()
, but both were unsuccessful. I searched online and somebody said this can be solved by invokeAndWait()
method but when I did so, the error message "Cannot call invokeAndWait
from the event dispatcher thread" was displayed.
Please help me with this. I am really new to these concepts. Thanks in advance.