• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swings, Threads, MVC: plz clarify  RSS feed

 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
Sorry for starting another thread:

I need to do lots of refactoring. Before I do so, was wondering if any of the Ranchers would be kind enough to provide feedback...

I have a singleton thread doing lots of changes to a treeModel & tableModel. I learnt that I was violating single thread rule of Swings after posting here:
JavaRanch Thread

However I need to know the following before doing more refactoring:

1> Should any GUI related changes be handled by UI thread including time consuming ones?

2> I was under the impression that changes to the model[tree or a table] can be done by any thread ofcourse doing it within a synchronized block. However representing these changes on the UIi.e. refreshing the UI needs to be handled by UI thread. Why does not this happen?

3> Swings are based on MVC paradigm. The advantage being seperation of Model and View. Are we not hindering this flexibity by insisting only one thread should alter the model of the UI.

May I need to understand the working of Eventdispatch Queue before jumping to conclusions. I would really appreciate any feedback from Ranchers on this issue.

TIA
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write an event listener that code runs on the AWT thread. It can safely update the UI. The UI is not responsive while the code is running, so if it's going to take very long you might want to run it on another thread. (This is not always feasible ... for example you may have a requirement to make the user wait until the operation is done.) When the other thread wants to update the UI it should use the invokeLater or invokeAndWait methods to do the actual changes on the AWT thread.

All this is what I infer from experience, not serious book learnin. Does it sound right to everyone?
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan has it right, although I like to use foxtrot, and SwingWorker found at the bottom of this page (which also has some good stuff about invokeLater and invokeAndWait).

SwingWorker was created by Sun years ago. Why it isn't part of the standard Swing library yet is beyond me.
 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan,

This is what I gathered after reading tons of material and from suggestions in this group:

Swings are based on single thread Model.
It has a EventDispatch Thread which performs tasks saved in EventDispatch Queue.

Now there are 2 methods to add tasks to EventDispatch Queue:
1> Through EventListeners and Adapters (as in when user presses a method)
2> Or through SwingUtilities.invokeLater()/invokeAndWait()

Hence all the custom threads which operate on GUI needs to be added to EventDispatch Thread using SwingUtilities.invokeLater().

In my case I had to do lots of refactoring. But atleast I learnt something

Ravi
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great attitude, man. Keep having fun!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New tech tip: http://java.sun.com/developer/JDCTechTips/2005/tt0419.html#1
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!