This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why doesn't the JTextArea refresh?

 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.
Will have a look in that.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
......
[ July 28, 2008: Message edited by: Maduranga Liyanage ]
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!
Thanks Rob.
 
Martin Wunderlich
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this dude it will be helpful for you...

Update JtextArea dynamically
 
shunmugha sundaram
Greenhorn
Posts: 2
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic