View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Sp2000 Midterm Review: Swing as MVC

Review at Midterm Review - Sp2000.

i'll take a stab at this one:

the mvc-ishness of swing varies between components. i'll use the JTree class as an example.

The model is easy since there is a rather aptly named TreeModel interface. TreeModel allows any underlying data structure to serve as the model as long as it defines the correct access methods.

The view is almost as easy. A JTree is a visual component and thus serves as the view.

We'll get to the controller in a minute, but first a complication. There is also a TreeNode interface and a TreeCellRenderer (and TreeCellEditor) interface. These serve as the model and view of an individual node in the tree, respectively. Thus a tree is a two part mvc object – the tree has a model and a view and each node has a model and a view.

the controller isn't quite as simple. according to the definition on 155 in mark's book, the controller is implicit since a java programmer can't modify the internal event loop which handles all user i/o (mouse, keyboard, painting, etc.). however, you can add a mouselistener (or any other kind of event listener) to the tree and add functionality by reacting in non-standard ways to events. thus you can extend the controller, but never really replace it.

dependencies are handled via listeners. you can register various kinds of listeners that will be notified when a specific event occurs (e.g. when part of the tree is expanded or collapsed). since listeners are implemented as interfaces, any class can become a listener.

working in the other direction is even easier. anyone can request that the tree updates itself by calling repaint(). thus, if the model changes by some means other than via the tree view, the controller can notify the tree by calling repaint() which will cause it to simply redraw the tree with the updated data.

note that not all classes follow mvc so well. for example, a JLabel object is both the model and the view. ostensibly, this was done because a JLabel is so simple, but it still isn't 'correct' mvc. other objects that do follow mvc well include JList and JTable.

i don't see any fundamental difference between models and views in java vs. those in squeak. ??? not sure about this part – i don't know why he would ask if there wasn't a difference...

mark: is this what you were looking for?

Very nice! Some notes
Mark Guzdial

Link to this Page