I am trying to implement a Passive View based on a gui system. Basically, I want my view implementation (the part that actually contains the swing code) to be minimal and do most of the work in my Presenter class. The presenter should not have anything to do with the swing, and should also "start the show", that is, tell what to do, and not vice versa.
I encounter problems when working with tasks with a large number of tasks and thread separation in general. I want the GUI updates to run on EDT, and the presenter logic to another thread. If I want the host to update the GUI part, it's pretty easy, I write something like this:
public interface View { void setText(String text); } public class Presenter { View view; ... public void setTextInVIew() { view.setText("abc"); } } public class MyFrame implements View { JTextField textField; ... public void setText(final String text) { SwingUtilities.InvokeLater(new Runnable() { public void run() { textField.setText(text); } }); } }
However, when the GUI needs to inform the host that some action has occurred, I want to disable EDT in response to it in another thread:
public class Presenter { ... public void buttonPressed() {
since actionPerformed code is run from EDT, so will Presenter.buttonPressed. I know that swing has the SwingWorker concept of launching tasks in another thread, however it looks like I have to insert the swing code into my host and the view starts the show. Any ideas how to solve this?
source share