Hi
Something with MVC that I cannot quite get is the separation of
Model-View-Control with the use of Java Swing components on the View and the
corresponding Component model.
E.g. View uses JTree and JTable. JTree has a TreeModel and JTable has a
TableModel.
If separating application into MVC, the View Class will have the JTree and
JTable components on it, but where
should the TreeModel and TableModel sit? In the Model Class, with the
Control Class wiring them together?
Has someone got a good (slightly better than trivial) example of this that I
could look at?
Thanks
Shane

Signature
Today I want to do something better than when I did it yesterday
Steven T Abell - 14 May 2004 04:55 GMT
> Something with MVC that I cannot quite get is the separation of
> Model-View-Control with the use of Java Swing components on the View and the
> corresponding Component model.
See http://www.brising.com/MVC.html
Swing gives lip service to MVC, and then does what it pleases.
If you find the result confusing, you're not alone.
Steve

Signature
Steven T Abell
Software Designer
http://www.brising.com
In software, nothing is more concrete than a good abstraction.
Remi Bastide - 14 May 2004 10:29 GMT
I'm sorry to say that I disagree with almost all that is said in this
web page.
One of the points I want to put forward is that being a view or a
controller is actually a ROLE that objects can play in your system. A
role is adequately described by a java interface.
Nothing prevents the same object from playing two different roles,
i.e. implementing two different interfaces. For instance, there's
nothing wrong in being both a view and a controller : this happens
when the semantics of the view and the one of the controller are so
correlated that it woul be pointless to try to reuse the controller
without its associated view. For example, the tree component in Swing
is both a view and a controller of the tree model. How would you reuse
the "controller" part without it's view ?
I advise everybody to think of MVC as a design pattern, and, as whith
other design patterns, there is a great flexibility of adaptation when
you use it in your own design. There's no reason to be religious about
one particular issue, such as "the view must be kept separate from the
controller" : adapt the pattern to fit your other constraints, as well
as your goals in simplicity and reusability.
>> Something with MVC that I cannot quite get is the separation of
>> Model-View-Control with the use of Java Swing components on the View and the
[quoted text clipped - 6 lines]
>
>Steve
Steven T Abell - 14 May 2004 16:37 GMT
> One of the points I want to put forward is that being a view or a
> controller is actually a ROLE that objects can play in your system. A
> role is adequately described by a java interface.
I'll agree with you on this up to a point.
But we see postings in this newsgroup fairly often
from people who are trying to make sense of the MVC idea
in terms of what they see in the Java widgets.
People can easily identify the view,
and have varying ideas about the model,
but what's a controller?
I've seen all kinds of things labeled MVC that aren't,
often resulting from attempts to create some kind of "controller"
because people don't generally know what one is
and they've been told they need one.
> How would you reuse the "controller" part without it's view ?
Easy: you'd reuse it with a different view
and save yourself the reimplementation effort.
Or you'd plug a different controller onto an existing view
and get different composite behavior.
Or you'd plug a different controller onto an existing view
and take your control information from another source.
As I said on that page you disagree with,
if you've never seen the real thing,
you'll have a hard time realizing what you've given up.
> I advise everybody to think of MVC as a design pattern, and, as whith
> other design patterns, there is a great flexibility of adaptation when
> you use it in your own design.
And there comes a point at which you're not using that pattern anymore.
The point of http://www.brising.com/MVC.html
is that, for better or for worse,
controllers are already part of most widget sets,
so don't break your head trying to invent one.
And if Swing's component models don't fit with what you've heard
about what models are supposed to be,
you're right: they aren't.
Steve

Signature
Steven T Abell
Software Designer
http://www.brising.com
In software, nothing is more concrete than a good abstraction.
Thomas Weidenfeller - 14 May 2004 08:53 GMT
> Something with MVC that I cannot quite get is the separation of
> Model-View-Control with the use of Java Swing components on the View and the
> corresponding Component model.
First of all, there is no original definition of MVC, others than that
Smalltalk people claim that what had been implemented in some Smalltalk
version is MVC. This is maybe the root of all the confusion. In order to
"get" THE REAL MVC you are supposed to look at a particular Smalltalk
implementation.
But even with that in mind, one can't claim that Swing does implement
MVC. It doesn't. Not even Sun claimed this. Quote "Swing architecture is
loosely based -- but not strictly based -- on the traditional MVC design.".
http://java.sun.com/products/jfc/tsc/articles/architecture/index.html
Like most "modern" GUI toolkits, Swing doesn't have separate
controllers. Swing basically has widgets, for the view and controller
part (parts of the controller job are also done by the event queue
mechanism). In addition, some of the widgets have corresponding models.
This architecture has a number of names, including Model-View, and
Document-View.
With this setup Swing gives you a chance to structure your application
somewhat MVC-like, or PAC-like, or whatever you want. If you go for an
MVC-like architecture, Swing, like most similar toolkits, suffers from a
granularity problem: If you really give each and every widget in a
non-trivial GUI an explicit own model, you end up which a large number
of objects and a lot of wiring (to get the event handling in place).
Dead GUI systems like InterViews or it's predecessor Fresco suffered
from this (Fresco has been re-surrected by the Berlin project).
Also, the pre-implemented Swing models most likely don't make up good
models in terms of what your application is all about. This should be
the problem you have with your TreeModel/TabelModel confusion; If you
want to represent the same data as a tree and a list you are either
forced to implement both of the interfaces or to implement some adapter.
Many people have written long articles about how to "fix" (circumvent)
the problems. There should be some good material at
http://www.jgoodies.com/ .
/Thomas
Steven T Abell - 14 May 2004 16:53 GMT
> First of all, there is no original definition of MVC
See www.ccmrc.ucsb.edu/~stp/PostScript/mvc.pdf
The invention of MVC is attributed by those who were there
to Trygve Reenskaug.
The name was the result of a discussion with Adele Goldberg.
Steve

Signature
Steven T Abell
Software Designer
http://www.brising.com
In software, nothing is more concrete than a good abstraction.
Shane Mingins - 16 May 2004 22:11 GMT
"Thomas Weidenfeller" <nobody@ericsson.invalid> wrote in message
news:c81tpq$h10
> Also, the pre-implemented Swing models most likely don't make up good
> models in terms of what your application is all about. This should be
> the problem you have with your TreeModel/TabelModel confusion; If you
> want to represent the same data as a tree and a list you are either
> forced to implement both of the interfaces or to implement some adapter.
Yes this sounds very much like my confusion. I kept thinking that I needed
to implement my own application model but then thought "but what about the
ones that come built to use with the Swing components?"
So it seems that the Swing model components are the "model" in the MVC of
the widget with the V&C combined in the widget. But when thinking MVC for
the application, ignore the widget models.
> Many people have written long articles about how to "fix" (circumvent)
> the problems.
This is what I am searching for .... to-date I have not found a good
article/demo that illustrates this with a slightly more-than-trivial
example.
>There should be some good material at
> http://www.jgoodies.com/ .
Tools and downloads .... not articles or demos ... or am I missing
something?
Cheers
Shane
Thomas Weidenfeller - 17 May 2004 09:54 GMT
> Tools and downloads .... not articles or demos ... or am I missing
> something?
There is less online than I thought.
http://www.jgoodies.com/articles/binding.pdf
is just a draft but contains a few pointers.
Fowler, Martin: "Analysis Patterns. Reusable Object models." has some
great information about structuring applications in general, including
n-tier applications and presentation logic (Chapter 12.).
I remember a short discussion with Ralph Johnson (of GoF fame) on some
web site, where he pointed out to me, that (a) one can view MVC as an
aggregation of several classic patterns - including Observer/Observable
for the View/Model link, and (b) that in complex systems one usually
ends up to recursively apply Observer/Observable. He was not happy with
me calling this recursive MVC. Others pointed out that this is known as
hierarchical MVC.
A common alternative is also to use the Adapter pattern to adapt an
application model to the interface(s) needed by the GUI.
/Thomas
Karsten Lentzsch - 18 May 2004 12:18 GMT
> Something with MVC that I cannot quite get is the separation of
> Model-View-Control with the use of Java Swing components on the View and the
> corresponding Component model.
As others have pointed out, you won't find the exact Smalltalk MVC
in Swing. But the Swing architecture shares the motivation behind MVC:
1) separate the domain from presentation and
2) decouple the model using dependents (observer/observable).
I will talk about data binding in a BoF at this year's JavaOne.
And I'm going to upload the presentation from the recent JAX talk.
It'll show up this week at http://www.jgoodies.com/articles/
The slides contain a metamorphosis from (All-together) to M-VC,
to M-V-C, to M-JComponent-VC, to M-JComponent-UIDelegate,
to Swing. And I try to categorize the different model portions:
data model, GUI state model, GUI properties.
It seems to me that many Java developers are confused about
the control behavior portion in Swing. I'd say part of the problem
is, that some people talk about controllers in a way that others
feel a need to implement controllers. If you truely understand
the event handlers in the Swing UI delegates, its fine to mix
the control and view roles in a single class. But I hesitate
to talk about controllers to not confuse developers; instead
I favor to talk about event handlers.
Anyway, I've found that the Swing control and view behavior
is almost irrelevant for the majority of Swing developers;
they can focus on the models and components.
In the talk I demonstrate different styles how to operate on
the Swing data state, GUI state and component properties.
> If separating application into MVC, the View Class will have the JTree and
> JTable components on it, but where
> should the TreeModel and TableModel sit? In the Model Class, with the
> Control Class wiring them together?
Separate the domain layer from the presentation layer.
Next, decide where you want to hold higher-level models
that adapt or convert your domain object properties:
in the presentation layer or in a model layer that
sits between the domain and presentation layers.
It seems to me that developers I talked to benefit most
from introducing a middle model layer. It helps them
understand where to put what code, where to do what.
My favorite 3-client tier architecture ends up with
a domain layer, with a presentation layer that builds
the UI using models provided by model holders that
lie in the middle model layer. All model operations
can then be performed in the middle layer. Most events
are handled in this model layer too. Only a few handlers
sit in the view layer and switch editable state and
other component properties. I will talk about this in
more detail at the JavaOne.
Choose appropriate patterns for mixing or separating
control behavior with state, or in other words separate
or mix control roles with model roles. In my opinion
a single pattern cannot cover all tasks, requirements,
and application sizes. Instead apply general OO techniques
for finding roles and building classes from these roles.
> Has someone got a good (slightly better than trivial) example of this that I
> could look at?
I'm in the process of writing a Binding Demo that
uses and compares different approaches how to bind
domain object properties to Swing components;
I bind single values and lists. Likely this demo
will be public at the JavaOne.
Only a few parties in the net publish articles around
Swing architectures, data binding techniques and how
to tie a Swing application together. You can find some
papers at Oracle's technology network. Google "JClient",
"ADF". Also google "Model View Presenter" "ValueModel Java".
Hope this helps,
Karsten