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:

enter image description here

  • You should check, how MVC is used in web apps, there it redirects. I am not to sure about swing, but maybe it is possible to have sth. like a WPF frame ;) So you basically have multiple JFrames and the just replace eachother, I think you need to write custom logic for that. – Knerd Nov 14 '14 at 11:04
  • Another thing, slightly OT, you should consider JavaFX. It is the successor of swing :) – Knerd Nov 14 '14 at 11:05
  • Oh thank you. I didn't realise this. Ive only been looking at tutorials and they use swing. I will look into javafx. Any links to some quick overview? – user3804927 Nov 14 '14 at 11:17
  • Actually oracle itself has nice tutorials :) Or just check wikipedia on it. And here – Knerd Nov 14 '14 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.