I’m pretty new in Java, but I’ve done plenty of research and I know what MVC is. However, to implement MVC in Java was a difficult task. I followed this tutorial, which helped me to realise that the event listeners usually embedded as an anonymous class can be segregated into a controller class – which makes it conform to the MVC pattern.
Now I’ve come across a problem when I try to add more code into the MVC design. My code has a MainController that takes care of the menu, toolbar, and MainPanel operation. Here’s the problem:
The MainPanel is to become a switchable workspace, where the component will change based on user action. For example – I have a Login component that will be placed in the MainPanel. After the user logs in, the Login component is replaced with the workspace component. The workspace component will then have interchangeable component depending on the work to be done.
So the question is – if each component within the MainPanel has their own controller, how do I pass the control back to the main controller and vice versa? That is, if a user clicks the Login button, (assume login was successful) what is the best way for me to call the MainController in order to instruct MainPanel to switch component from Login to something else?
Bonus question: If I need to pass control from LoginController to say the workspace controller, should I always go through the MainController, or is there a way for them to communicate directly?
Here is a UML class diagram to depict what I have at a high level:
4