I would like to apply the MVC design to my Java application using Swing in a meaningful way. Therefore my question is, how the controllers would be structured in Java Swing?
I have two options in mind:
- Every Component Listener is an own class, as part of the controller package
- Every Component Listener is an anonymous class inside the view package which delegates its call to a class with controller methods.
Is both possible? Is it a question of preference, or is it clearly defined?
The view would encompass the GUI design. The actionPerformed method of the view should identify the event, and call respective methods on the ControllerInterface depending on the event. The controller implementation will implement this ControllerInterface, and the methods which are called by the view will be implemented here. These methods will presumably interact with the model in some way. The model itself will have a way to register observers (the view, in this case; incidentally, even controllers can be registered) and update observers each time the model changes. That, in essence is how you would structure your application. For more details on MVC, you can also refer to: here!