• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing Threading - how to force it to run?  RSS feed

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

I am busy writing a client server applications. The results of the communication need to be displayed in a JTextArea.

Currently I have the communication working between the client and server on Threads and the update of the JTextArea is on a separate Thread.




Now what I am finding is that the message gets printed out to the System.out from inside that run method, but it doesn't update the screen.

I know it has to do with the fact that the swing components are running on their own thread, and I read that placing the code that update's the components into a Thread would solve this, but I'm still struggling.

Can anyone give some advice?

Thanks,
Rachel
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rachel,

I don't know if this solves your problem, but you can use
SwingUtilities.invokeLater (or something like that) which
is safe to be called from a different thread.

But I guess in this case the problem is somewhere else (even
though nothing strikes me in your code).

Best regards,
Petr
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about that but I read in the API that

"the doRun.run() will still be deferred until all pending events have been processed"

So I don't think that solves the problem. But thank you !

Rachel
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rachel,
just a couple of suggestions.

You could set up a swing timer and use it either to:

Schedule your tasks one at a time so that they can pass messages to display in the text area by using invokelater. You would need some class counter value to point at successive jobs to be done and use a switch statement to select the right job on each timer tick. I use even counter values to jump to particular jobs, increment the counter and then I let the job increment the counter again so the next timer tick calls the next job. I have used this method several times with minimal effort.

Or:

If your jobs are long and running on another thread, use a queue to pass your messages (the ones you want to display in your text area) along to a swing timer which check the queue and removes and displays the messages. I haven't used this method myself yet, but I intend to very soon.

Hope that helps.

Ed

[ August 20, 2004: Message edited by: Eddie Vanda ]
[ August 20, 2004: Message edited by: Eddie Vanda ]
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ed. I'll give that a try over the weekend!

Cheers,
Rachel
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!