0

I created a Java application that kept basketball stats for a team of players. The first time I made the program, the GUI elements created actions that were dealt with in the same class, then the players were modified.

I am interested in rewriting the program following an MVC model. I assume I will need a view or GUI, then a model (Player), and a controller.

The controller is hard to understand for me. How do I pass what happened in the GUI to the controller? For instance, in the old program, I had 5 JComboBoxes and I would have an Array of int in order to keep track of the active players. The currently selected player would have his stats updated if an event was recorded under his name.

How would I go about this using the MVC model, would I need an instance of Controller for each Player?

Here is a picture of the old GUI so that you can visualize how the program works.

StatKeeper program

Thank you for any suggestions.

Trevor Hutto
  • 2,112
  • 4
  • 21
  • 29
  • 1
    Swing components are already MVC.. When you click a button .. the listener is the controller for example `Action` class. – nachokk Feb 11 '14 at 17:47
  • I had a driver class, a GUI and Player in the old version. Everything was slammed into the GUI class, I feel like there is a better way to do it. But you are saying there is not? – Trevor Hutto Feb 11 '14 at 17:48
  • see [this SO question](http://stackoverflow.com/questions/8693815/java-learning-mvc) about Java MVC – dev_feed Feb 11 '14 at 17:54
  • nah you don't wanna put everything in one class. the MVC idea is just an architecture / design for coding – dev_feed Feb 11 '14 at 17:54

1 Answers1

0

You can implement the MVC architecture in a variety of ways. Instead of having an instance of controller for each player (model), you could have the controller be a stand alone instance and accept the model and view. So, one implementation in pseudocode:

playerList = new model();
gui    = new view(playerList);
handles = new controller(playerList, gui);
dev_feed
  • 689
  • 2
  • 7
  • 25
  • So you would suggest the model being a list of Players, instead of a single Player? – Trevor Hutto Feb 11 '14 at 18:19
  • @TrevorHutto not in implementation, but as far as generalizing, yes. typically the playerlist would be the database and it would contain data about instances of models (which would be just one player per record) – dev_feed Feb 11 '14 at 18:27
  • @TrevorHutto in other words, the model class would be Player. But you would pass your list of models to the view – dev_feed Feb 11 '14 at 18:28
  • The list would be kept in the controller? Or in a separate class? – Trevor Hutto Feb 11 '14 at 18:33
  • The list would be kept in your driver / main class (edit: if you're not using a database) – dev_feed Feb 11 '14 at 18:35