title: How to manage view updates from controllers in a Java Swing app
body: <p>I'm finding that writing good OO code with Swing is incredibly hard. My problem is essentially that I have a view (a JPanel) that has action listeners. The action listeners figure out which button was clicked and call the appropriate controller method. The issue is that this controller method needs to update another view. So the issue I'm having is I have views being passed all over the place to controllers. Here's an example.</p>
  
  <pre><code>public class MyView extends JPanel implements ActionListener {
    private final MyController controller = new MyController();
  
    @Override public void actionPerformed(ActionEvent e) {
      this.controller.updateOtherView();
    }
  }
  </code></pre>
  
  <p>This is essentially what I want, but this is what ends up happening.</p>
  
  <pre><code>public class MyView extends JPanel implements ActionListener {
    private MyController controller = new MyController();
    private OtherView otherView;
  
    public MyView(MyOtherView otherView) {
      this.otherView = otherView;
    }
  
    @Override public void actionPerformed(ActionEvent e) {
      this.controller.updateOtherView(otherView);
    }
  }
  </code></pre>
  
  <p>And you can see that as the number of views that need to be updated increase and the number of classes that look like this increase, the views are essentially global variables and the code becomes complex and unclear. Another problem I'm running into is this other view usually isn't directly passed into MyView, but it has to go through the parents of MyView to get to MyView, which really just bugs me.</p>
  
  <p>For a real example of this, lets say I have a Menu and this MyView. MyView has a play button, which plays some music for a while and it disables (greys out) the play button until the music is finished. If I have a menu option called play, now I need to access the other views play button so I can grey it out. How can I do this without this annoying passing of views everywhere? Although there might be specific solutions for this problem, I'm looking for something that will solve this view access problem in the general case.</p>
  
  <p>I'm not sure at all how to fix this. I'm kind of using MVC pattern terminology at the moment without using the MVC pattern, which may or may not necessary. Any help is appreciated.</p>