The Swing Model-View-Controller Architecture (MVC) is the model behind all the swing components (JButton, JMenu, etc). You may have noticed that there are 3 parts to the MVC Architecture:


The Model is where the data for the component is stored. For example, in a JButton the button label is part of the button's model. In a JList, the list data is in the List Model. Ok.


The View is the visual representation of the model. The view makes up half the interface to a component - it graphically represents the model to the user


The Controller makes up the other half of the component interface, mainly the interaction half. The Controller takes care of mouse and keyboard events.

An Example...

An example/analogy might help here, so let's use a JList and a computer. The JList has a model - in fact, it's called a ListModel. The ListModel is pretty much a Vector of the different strings you want to display in the JList. In the computer analogy, the model is the hard drive. In both the hard drive and the ListModel, you can store and change data, but you can't actually "see" it. This is where the View comes in. In a JList, the view is the JList itself - the thing you see in VAJ when you draw it. In a computer, the View would be the display - that was sort of obvious. Last but not least is the Controller. In a JList, the Controller is a set of mouse and keyboard listener functions. In a computer, the Controller would be.....the mouse and keyboard! (bet you didn't see that one coming).

Why would anyone care about this?

Well, if all you want to do is have some buttons, the MVC architecture isn't going to help you much. But most programs need to display things like lists and trees and tables. The Models for these components are specifically designed to be easy to work with. So you probably want to use them. Oh yea, I forgot to mention - the Model takes care of telling the View when to redraw itself. So you don't have to worry about it. Just add something to the ListModel and the JList will change on the screen. Like Magic =)

Back to the Tutorial