File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Why doesn't the JTextArea refresh? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why doesn Watch "Why doesn New topic
Author

Why doesn't the JTextArea refresh?

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hello,

I Googled around but seems although a popular problem, there isn't a solid way of rectifying it.

So here is part of the code (an inner class):



What I basically do here is take an user input form a JTextField and run in the command line.
The problem is, my JTextArea(commandDisplay) does not update until the oprationis over.

For example, if I do "ping -c 10 www.google.com", 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();

Thank you.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Yes, it's simple. Just don't do that in Swing's event thread. Do it in a separate thread. Here's a link to the tutorial about that:
Concurrency in Swing
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks Paul.
Will have a look in that.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
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.


bitguru blog
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
......
[ July 28, 2008: Message edited by: Maduranga Liyanage ]
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
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..

Thank you.

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().

Cheers!

[ July 28, 2008: Message edited by: Maduranga Liyanage ]
[ July 28, 2008: Message edited by: Maduranga Liyanage ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Got it!
Thanks Rob.
Martin Wunderlich
Greenhorn

Joined: May 06, 2006
Posts: 9
Hi all,

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.

I have tried to replace this with the following:


This doesn't work either. What am I doing wrong??

Kind regards,


Martin
shunmugha sundaram
Greenhorn

Joined: Jun 05, 2009
Posts: 2
check this dude it will be helpful for you...

Update JtextArea dynamically
shunmugha sundaram
Greenhorn

Joined: Jun 05, 2009
Posts: 2
http://technobuz.com/2009/05/update-jtextarea-dynamically
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why doesn't the JTextArea refresh?