Jump to content

Java Swings/MVC

From Wikibooks, open books for an open world

This chapter explains how to separate Swing applications into a more maintainable triad of components: the domain model, the view of the user interface, and the controller that oversees these. A minimal class structure is shown below.

File:Java Swings Wikibook, MVC class diagram.svg

The main application class is the controller: it creates a model object (which is Observable), and a view object (which is an Observer of the model). The controller adds the view as an observer of the model.

When the model changes, it calls setChanged() and then notifyObservers(). This means that the model can react to user input.

The controller doesn't touch the view (apart from instantiating it and giving it the model).

The view has access to the model, including to change it.