Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invokeAndWait()/invokeLater() ??

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application I have a JTree that I am going to update every xxx minutes. So I made this class:

Now I have a class that extends JPanel and in that class I build my tree. After I build my tree I then want to go ahead and start the Runnable class so that the tree will update every xxx minutes.
So I do this:

I have also tried

The problem is that this Runnable starts running, but I never get returned to the GUI. Everything in the GUI just hangs while the Thread runs.
Any ideas?

BTW - This is a JApplet in case that makes a difference.
 
Shyam Purkayastha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shold not use invokelater().Since invokelater() executes the thread in the event dispatched thread and as your thread runs in an infinite loop it will never end and your application will hang.
Why dont you run it as a seperate thread alltogether.
new Thread(Runable/*your Runnable class*/).start();
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that seems to work good.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is right to do the updating in the AWT-main-thread. so I'd suppose it's safer to do this:

when you catch the interrupted exception you should call "Thread.currentThread().interrupt()" - unless the current thread is the AWT-main-thread, of course. otherwise the thread does not die.
this polling thread will most probably never be interrupted, but for data retrieval in a gui, it is important.
see this very good article on stopping threads:
http://www.javaspecialists.co.za/archive/Issue056.html
Chantal
[ November 25, 2002: Message edited by: Chantal Ackermann ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic