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.

Robert McCoy
  • 661
  • 1
  • 10
  • 19
  • 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 – MadProgrammer Mar 21 '17 at 23:18
  • That's kind of what I figured. Is there a good "Java MVC Best Practice" documentation anywhere? – Robert McCoy Mar 21 '17 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 – MadProgrammer Mar 21 '17 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](http://stackoverflow.com/questions/31576623/how-mvc-work-with-java-swing-gui/31576899#31576899), [this example](http://stackoverflow.com/questions/26789848/implementing-the-controller-part-of-mvc-in-java-swing/26790064#26790064), [this example](http://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919) – MadProgrammer Mar 21 '17 at 23:31
  • [this example](http://stackoverflow.com/questions/35032048/jtextfield-input-fails-to-update-output-in-textview-in-mvc/35033580#35033580) and [this example](http://stackoverflow.com/questions/26517856/java-and-gui-where-do-actionlisteners-belong-according-to-mvc-pattern/26518274#26518274), which attempt to answer specific requirements using a MVC wrapped around a MVC (Swing) – MadProgrammer Mar 21 '17 at 23:31
  • See also this [Q&A](http://stackoverflow.com/q/25502552/230513). – trashgod Mar 21 '17 at 23:32
  • These are some really good resources, thanks all. – Robert McCoy Mar 21 '17 at 23:36

0 Answers0