Java SE MVC implementation with Swing

I implemented the MVC pattern for Java SE with Swing using PropertyChageSupport and PropertyChageListener . The diagram for the implemented MVC is as follows.

Modified MVC Pattern

In the implementation of View I change the property in Model using Controller .

View contains the following code for the Ok button.

 btnOk.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { modelController.setNumber(Integer.parseInt(numberField .getText())); modelController.setName(nameField.getText()); } }); 

Full code can be found in SwingMVC .

Now, my question is: I am writing the above code for btnOk in View or should I write it in the Controller method so that in View , I'll 'do

 btnOk.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { modelController.btnOkActionPerformed(); } }); 

From the above two implementations, which is the preferred way to implement MVC?

+2
source share
2 answers

First a warning: I'm not a professional or a student, but the bravest one, but having said that, my preference belongs to your second example,

 btnOk.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { modelController.btnOkActionPerformed(); } }); 

The control will have to call methods in the view to retrieve information, and any methods that it calls will be part of the interface that implements the view. My goal in this is to keep the view as deep as possible and do almost everything to make communication as easy as possible.

+4
source

Your diagram offers a model-view-presenter (MVP) template that is compatible with Swing Design Development . In this context, Action is a convenient way to encapsulate the functionality of an application for export from your model. As specific examples:

  • DefaultEditorKit and StyledEditorKit export useful Action types that work with the Document model common to text components. As shown in this example, such actions update the Document , which indirectly updates the corresponding component of the view.

  • ControlPanel in the example here shows several Action instances that work directly on the implicit List<Node> and List<Edge> models.

+4
source

All Articles