1

I'm trying to implement MVC in my program, this is currently my Controller class:

public class Controller {

    private DatabaseModel model;
    private View view;

    public Controller(View view, DatabaseModel model){
        this.model = model;
        this.view = view;

        this.view.getMainPanel().getCandidateForm().
                  addSubmitListener(new CandidateListener());
    }

    class CandidateListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.out.println("ACTION");
        }
    }
}

because my view includes several private sub JPanels - MainFrame and CandidateForm - with the latter being where the button is, at line 10 i pass the candidateForm panel back up to the controller and then call it's 'addSubmitListener()' method, which adds the custom listener to the button.

does this seem like the correct way to add the listener? should i be defining the CandidateListener this way in the Controller? or maybe make an anonymous class in the sub JPanel?

Any other advice on best practice for MVC and listeners would also be appreciated!

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
NickW
  • 1,207
  • 1
  • 12
  • 52
  • 1
    Possible duplicate of [Correctly implementing the MVC pattern in GUI development using Swing in Java](http://stackoverflow.com/questions/25502552/correctly-implementing-the-mvc-pattern-in-gui-development-using-swing-in-java) – Catalina Island Jan 23 '17 at 11:27

1 Answers1

1

As noted here, "not every interaction needs to pass through your application's controller." Your approach is not wrong, but it may scale poorly. Consider using Action to encapsulate functionality, as suggested here. In a database context, this simple example creates an Action that adds a query result tab to a JTabbedPane. This more elaborate example uses a SwingWorker to query a database in the background; a corresponding Action might instantiate the worker and execute() it.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • wow, thanks. that database example is particularly good. i'm not great with event handling in general either, i'm pretty new, but that helps a lot. as for every interaction passing throught the controller - is that why in the color guesser example you passed the model to the view? sorry for the late reply, the whole thing kind of got put on the back burner and i forgot i asked. but it's appreciated! – NickW Jan 30 '17 at 07:13
  • @nickW: As suggested in this [outline](http://stackoverflow.com/a/2687871/230513), the view may hold a reference to the model. – trashgod Jan 30 '17 at 07:26
  • nice, i'll definitely look at that later. i've enjoyed reading your code examples so far. 1 quick question about that diagram though - i'm assuming the dotted lines are *not* references/links (they're the indirect communication, right?) – NickW Jan 30 '17 at 10:17
  • @NickW: Yes, typically using the observer pattern; the model knows nothing about the view except how to add a listener. – trashgod Jan 30 '17 at 10:22