user2465510 user2465510 - 9 months ago 24
Java Question

Changing text in JTextArea

I'm trying to change the text in JTextArea whenever the following PRINT() method is run. The entire program is long so I can only post a bit but basically a [50][50] char array contains is being turned into 50 lines of 50-character strings. The JFrame holding this JTextArea is initiated when a button is pushed and immediately prints the char array into the JTextArea (when I've made it so that the char array is never updated). The program is meant to update this text every few seconds.

Instead of updating every second, though, The JFrame appears as basically a blank box until the last time PRINT() has been run. Currently, it is meant to print 1...10 in separate lines through 10 calls to PRINT(), each after output.outputLines has been updated with the added number. Instead, it only shows a blank frame until the last number has been loaded, then prints the whole thing.

PRINT() method:

public boolean PRINT(){

String result = ""; //Used to hold the characters to be printed to the screen
for(int x = 0; x<50; x++){
for(int y = 0; y<50; y++) {
result = result + String.valueOf(output.outputLines[x][y]);
}//End of inner FOR statement

result = result + "\n";
}//End of outer FOR statement

return true;

code used to pause between PRINT() calls:

try {
catch (Exception ex) {
System.out.println("Exception in Thread.sleep()");

Can anybody help me to figure out what is going wrong?


You're calling Thread.sleep(...) on the Swing event thread, essentially putting it and your GUI to sleep. The solution (as always) is don't do this. If you want to pause use a Swing Timer. If you need to run long-running code, use a background thread such as a SwingWorker.