Originally posted by Maduranga Liyanage: the text area doesnt show the 10 ping results until the operation is over.
But I can see my terminal getting updated every time (System.out.println(line+"\n") )..
Is there anyway I can work around this problem easily? It is rather surprising that Java doesn't have a method to do it.
I already tried the following: commandDisplay.updateUI(); commandDisplay.redraw(); commandDisplay.revalidate();
Do you mean repaint() instead of redraw()? No matter. Get rid of all the updateUI/repaint/revalidate stuff, since append() alone will suffice if you just give the Event Dispatch Thread time to repaint the screen.
Your code locks up the EDT by performing the exec() on it, so the EDT can't repaint the screen until the exec() finishes.
Simply launch another thread to do the exec(). JTextArea.append() is even documented to be thread-safe, so the other thread can call it without worrying about other threading issues. It couldn't be simpler.
I did the following but the problem is still there.. what am I doing wrong?
They are defined as two different inner classes.. but still the GUI isn't getting updated line-by-line..
P.S: It works! Sorry I had initialized a wrong object. Thanks a million for the help guys. I would really be glad if someone can clarify me what advantages I have with doing InvokeLater() than doing this way? I can't seems to really grasp the idea of invokeLater().
[ July 28, 2008: Message edited by: Maduranga Liyanage ] [ July 28, 2008: Message edited by: Maduranga Liyanage ]
Originally posted by Maduranga Liyanage: I would really be glad if someone can clarify me what advantages I have with doing InvokeLater() than doing this way?
Unless the API specifies that a method in Swing is thread safe (like JTextArea.append is), you should call all updates to the user interface using invokeLater or invokeAndWait. [ July 28, 2008: Message edited by: Rob Prime ]
Sorry for reviving this oldish discussion. I have run into the exact same problem, with a JTextArea not getting updated as I append lines of text. I have read through this post and some others on the same topic, but I still can't get it to work.
Here is what I have done. Assuming I have the following code, which logs a message to the JTextArea:
This is all part of a larger method that gets executed after hitting a button in a Swing GUI and is run a couple of times in a loop. The JTextArea gets updated only after the whole loop is completed.