1

So I have read a lot about MVC online and have learned about it in class, but I am still lost on one aspect - changing and showing Views. I know Views are GUI, they pass user input to the Controller, but I'm having a hard time wrapping my mind around how the following would work:

  • View A displayed
  • user clicks button on View A
  • Controller notified, tells Model
  • Model tells Controller to display View B
  • Controller displays View B?!?

The last 2 lines here is what I don't understand how to implement. If the View did not change to another View, I know to use the Observer/Observable interface to update the View. But in my case there is a Home Screen and a Game Screen and when the user clicks Play button on the Home Screen, I want the "view" and the GUI to change to the GameScreen. I want to use 2 distinct Views (I think).

I'm having trouble structuring my code to achieve this, and I don't know where to put the ActionEventListeners

3
  • Your model explicitly asks the controller for another specific view? If so, its not the way to do it. One of the advantages of MVC is that you can supposedly switch your views to work on the same model (like a desktop or web ui) Commented Apr 24, 2015 at 13:25
  • 1
    Can you post the relevant code? Otherwise we would be shooting in the dark. Commented Apr 24, 2015 at 13:25
  • I'm still in the phase of creating UML class diagrams and sequence diagrams, so I don't actually have any code yet Commented Apr 29, 2015 at 0:57

1 Answer 1

2

Assuming you're just switching the view, this is the sequence.

  • View A displayed
  • User clicks button on View A
  • Button controller tells view to display View B
  • View displays View B

The model is not involved at all. Other controllers can change the model.

When coding a Java Swing application, here's what I do.

  • The view may read values from the model.
  • The view may not update the model.
  • The controller(s) will update the model.
  • The controller(s) may revalidate / repaint the view.

To see an example of the model / view / controller pattern in a realistic Swing application, take a look at my article, Retro Snake Game.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! This is much clearer. I didn't see how the Model was supposed to dictate the View to be displayed when (I thought) it wasn't supposed to know about the Views at all.
@Mimi Sakarett: The model doesn't know about the view. The view, however, knows about the model.
I'm assuming there's some parent view which has ViewA and ViewB imbedded in it somehow(maybe through CardLayout), and you pass an actionlistener from the controller to ViewA telling the parent view to switch to ViewB in case of a button click. Is my understanding correct?
@hidden_machine: I think I answered your other question with a comment. You have a JFrame, with a main JPanel that uses a CardLayout. ViewA and ViewB are subordinate JPanels to the main JPanel. The controllers are defined within the view classes if they're short and simple lambdas and in separate classes if they're more complicated. If the controller is a separate class, you pass an instance of the model and the view to the controller. The model comes first and foremost, then view, then controllers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.