0

I understand the basics of MVC applications, but Java desktop MVC applications with Swing are really giving me a headache. I mostly do Web MVC so some of the crossover just doesn't make sense. I understand basic implementation with a very simple layout (E.g. this), but expanding it outwards and including things like a game board or other swing elements and I start to get confused because of the scope of objects.

Let's say I'm making an application that should have a 9x9 GridLayout (For now we'll just say they're "Tiles" and they should have a value that would be displayed). The numbers that would be placed into these tiles come from another Swing component (like a queue of sorts), and then the game board itself has some information like score, time, moves, etc. and some buttons.

How do all the MVC components fit together here? How is the game board itself done with MVC? Does it just have a TileController object, and that TileController has a TileView that holds a 2D Array of TileModel objects (and then this is repeated for the other things like the queue)? How do you then make the TileController have the TileView (which would have a 2D Array of TileModels) and TileModel work together, or should the controller even have a reference to the Models at this point?

These questions just keep branching, and if I try to rewrite my UML each time I confuse myself even more with all of this, because at the core it's very simple, but implementing multiple views and controllers across a game seems very complex.

7
  • Swing is "based" on the MVC paradigm, although implemented more along the lines of M-VC, where the view is it's own controller, at least while you're not working with the look and feel delegates. There's no "right" answer the question, what you want to do is look at decoupling the code enough that it's easy to replace any one part without adversely affecting the others. You can have a view act as a controller for other views, so in the case of your game board, it could be the controller for the titles. Equally, you could have compounding/proxy models, which feed off a parent model Commented Mar 21, 2017 at 23:18
  • That's kind of what I figured. Is there a good "Java MVC Best Practice" documentation anywhere? Commented Mar 21, 2017 at 23:23
  • If there was, it would be a nightmare :P - because server side MVC is going to be different from client side MVC and then when you need to blend the two, you have another MVC to manage that :P. When it comes to managing Swing in a MVC way, I focus more on functionality then I do on implementation. That is, I focus on the intention of the view and the model and not how they might actually be implemented, this makes it very flexible and disconnects you, in part, from the Swing API, my controller doesn't care how an action is triggered, only that the view can trigger it Commented Mar 21, 2017 at 23:25
  • The subject of MVC and Swing is large and there is no "right" answer, this makes your particular question quite broad in it's context. With that in mind, you could have a look at this example, this example, this example Commented Mar 21, 2017 at 23:31
  • this example and this example, which attempt to answer specific requirements using a MVC wrapped around a MVC (Swing) Commented Mar 21, 2017 at 23:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.