0

I have read about MVC but am having doubts on how to implement the concept in Java with Swing. Here's what I'm going for:

The model: ListOfThings contains a Collection of Thing objects.

The controller: Controller instanciates ListOfThings and populates it with a "add" method (internally creates a new Thing and adds it to the Collection)

The view: A Swing interface with a ListOfThingsPanel which will contain ThingPanel components to represent the model. Both extend JPanel. ThingPanel contains various components meant to display the data of the Thing it's linked to. It also has a button which adds a new (empty) thing to the list. The click event calls the Controller's addThing() method which asks ListOfThings to add a new Thing to its list. ListOfThings has an event/listener system and ListOfThingsPanel listens to it to know when it should refresh the view.

Am I properly following the MVC concept by doing it like this?

Update: I'm still learning Java but I have coding experience and would prefer to learn the theory and attempt it by my own means before using premade frameworks. While I'm aware Swing implements the MVC pattern, I have read it does it in a specific way (View and Controller combined) which might not be the best and not applicable in other circumstances. I'm a bit wary until I can make sure "Swing MVC" is not different from "MVC", or that the differences won't impact my understanding of the underlying ideas.

One thing of import to me is to learn to really separate the model from the view to allow me to ultimately create various views of various types (Swing but also console or update to JavaFX for example) This is why I'd like to avoid anything Swing specific outside of the view part itself.

leokhorn
  • 505
  • 1
  • 4
  • 11
  • Swing uses an MVC architecture too..I would simply follow their lead. – mre Jan 29 '13 at 16:59
  • Seems fine. Do you want to change (modify) Things or delete (remove) Things? – Gilbert Le Blanc Jan 29 '13 at 17:00
  • More [here](http://stackoverflow.com/a/3072979/230513). – trashgod Jan 29 '13 at 17:02
  • Also consider JavaFX instead of Swing for new GUIs, which is pushed by Oracle as the new client side technology for Java. – Puce Jan 29 '13 at 17:22
  • @mre I read here and there that Swing, as is, combines parts of the MVC (View and Controller I think) so I'm a bit scared it won't teach me "proper MVC". – leokhorn Jan 29 '13 at 19:33
  • I've worked with many different GUI toolkits in different languages and the "proper MVC" pattern is rarely used. (I think I tried it once with JSP + Servlets, though.) It usually looks weird and over-engineered if people try to apply MVC just because they learned about it. See my answer below. – Puce Jan 29 '13 at 23:32
  • @Puce So using pure MVC would be overkill? – leokhorn Jan 30 '13 at 10:59
  • @trashgod Thanks, I'll have a look. – leokhorn Jan 30 '13 at 11:00
  • @GilbertLeBlanc Things will definitely be added, removed and modified but I think I'll open a separate question on how best to deal with updating the list. – leokhorn Jan 30 '13 at 11:03
  • @leokhorn Most of the time, yes, apart from what a framework such as Swing offers. As stated in my answer below use a combination of POJOs, models and custom JPanes (as Mediators). Only apply design patterns if you need their benefit. – Puce Jan 30 '13 at 11:32

1 Answers1

2

While the Swing framework already implements a form of MVC (explicit models; JXyz & UI classes = controller & view), this strict separation is rarely used on application level and looks rather weird.

To start I suggest to follow the following design:

  • implement the client-side business logic with POJOs
  • wrap the POJOs with custom Swing models where needed (ListModel, TableModel)
  • Use a GUI builder to design the GUI
  • Use the Mediator pattern to listen for events (a custom subclass of JPanel listens for events of its children and updates other children or fires own custom events if needed)

If you want to go a step further, use a RCP such as the NetBeans Platform (very recommended).

Edit:

And here is the article explaining MVC in Swing:

http://www.oracle.com/technetwork/java/architecture-142923.html

Puce
  • 37,247
  • 13
  • 80
  • 152
  • You're pointing exactly at what's bothering me: a specific implementation of MVC by Swing rather than the general pattern (updated my question to reflect this). Isn't ListModel also specific to Swing? Won't it link the model to Swing when it's supposed to remain unaware of the View implementation? (again, I'm still learning, thus my being prudent here). I'm using Netbeans and its GUI builder. I try not to be too dependant on the latter and understand what's going on behind the scenes though. – leokhorn Jan 29 '13 at 19:51
  • You could use the same ListModel with several JList instances, but this is rarely used. But using custom models will make your code cleaner and your GUI will perform better. The model on the other hand can use POJOs, which can be used anywhere. Here is a sample of read-only TableModel using POJOs, which I wrote some years ago: https://sourceforge.net/p/puces-samples/code/13/tree/tags/sessionstate-1.0/sessionstate-suite/sessionstate-sample/src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java – Puce Jan 29 '13 at 21:51
  • Also see my update answer, which provides a link to the article explaining MVC in Swing. – Puce Jan 29 '13 at 23:24