2

I am writing a Java Swing application in MVP style. The model works on various calculations in a different thread that are not relevant for this question. When these updates occur, I call EventQueue.invokeLater to update the GUI. Should these thread-switching calls occur in the Presenter or in the View?

Argument for Presenter:

  • The view should be as dumb as possible
  • If the view does it, it's theoretically possible for the view to call EventQueue.invokeLater from the EDT, which is wasted work

Argument for View:

  • It is not necessary to have a dependency injected Executor for unit testing the Presenter
  • The View is responsible for rendering itself, ensuring that updates happen on the EDT is part of that responsibility

I'm not certain. What's the best practice?

durron597
  • 30,764
  • 16
  • 92
  • 150
  • use invokeLater for Swing methods only, real method implemented in Swing APIs (e.g. setText, not whole code block with setText inside...), doesn't matter where they are placed (your code), this is part of your desing, if code is readable, good and logically structured, which has nothing to do with EDT – mKorbel Aug 02 '13 at 06:29
  • @mKorbel Wait a minute, from your response it sounds like "only put the actual Swing calls on the EDT" which means this offloading should clearly be in the View code. (as opposed to "it doesn't matter") – durron597 Aug 02 '13 at 13:19

1 Answers1

0

My understanding of MVP is that you would want your Presenter to talk with your View through a separate interface. See this question for a good high-level explanation. That interface is where your calls to invokeLater should go. This will make unit tests easier to write.

Also, just FYI, you can always call out to EventQueue.isDispatchThread() to check before you make any calls to invokeLater or invokeAndWait. InvokeLater won't hurt anything to call it from the EDT, but invokeAndWait would throw an exception.

Community
  • 1
  • 1
allTwentyQuestions
  • 1,120
  • 7
  • 11