• Post Reply Bookmark Topic Watch Topic
  • New Topic

AWT and Threads

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi...


I'm trying to understand how I could implement the following in AWT(This is a project that my friend was given in his Computer Science course last semester that I am curious to implement).The idea is to have two different threads,each updating the GUI.Each Thread should display a straight line that grows according to time elapsed.I tried to do this first like this(sorry this is from my local history and may not be 100% correct,but it should serve to illustrate what I want to do):



But, I think there is some sort of blocking happening with the paint method as I only ever saw one line getting drawn.What is the correct approach to do something like this?I'd like to figure this out as there are times when I may need to write multithreaded awt components for my job(writing MHP applications.....or trying lol)
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Concurrency in Swing. It also applies to AWT.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I tried to instead fire the threads by using java.awt.EventQueue.invokeLater(Runnable) but that doesn't seem to help.

I don't understand how this is possible actually.As far as I understand there is only the EventQueue thread that performs paint() operations,so regardless of whether or not I fire off multiple threads to update the GUI at the same time(multiple paint() methods associated with multiple components) they are still going to execute sequentially as it is a Queue that is handling the painting....What am I missing here?
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't even call paint in a different thread. The results are undocumented.

If you need to have a regular repaint you should check out javax.swing.Timer. This fires new events on the Event Dispatcher Thread.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I'll check it out...thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!