Page 1 of 1
Implementing MVC with Swing (Java) ?
Posted: Sun Oct 22, 2006 5:38 am
by Chris Corbyn
I'm struggling to see a clean way to using Swing as the View part of MVC. The problem mostly is that Swing handles user events (OK technically it's AWT) as well as view components.
How do you go about separating controller from view?
I'm kinda wanting to do something like:
Code: Select all
import myapp.*;
/**
* Main application entry point
* I guess this could be a front-controller
*/
class MyApplication
{
protected GUI gui;
public static void main(String[] args)
{
this.gui = new GUI();
this.gui.setController(new myapp.Controller());
this.gui.setModel(new myapp.Model());
this.gui.render();
}
//Rest of class
}
But I don't see what should go in Controller. GUI already manages events since they are attached to its components. Do I just make Controller implement <whatever>Listener and attach controller to Swing components?
It's a bit weird trying to get used to writing apps without the request/response type setup. Then again JavaScript is like that.
Posted: Sun Oct 22, 2006 9:28 am
by timvw
I found this matter confusing too... In the end i decided to have the following setup:
application = data + logic + representation
data = sql dbms + webservice + filesystem + ...
logic = business rules + data access logic
representation = view (JComponent and subclasses) + controller (display logic) + model (gateway to business rules / data access logic)
Posted: Sun Oct 22, 2006 10:02 am
by Chris Corbyn
timvw wrote:I found this matter confusing too... In the end i decided to have the following setup:
application = data + logic + representation
data = sql dbms + webservice + filesystem + ...
logic = business rules + data access logic
representation = view (JComponent and subclasses) + controller (display logic) + model (gateway to business rules / data access logic)
I see. So it's not really full MVC (not that it's overly important). Swing is fairly good in terms of helping to separate the model from everything else but the rest is a sort of hodge-podge together. Even if you try to break of the event listeners into their own classes you'd finish up with a sickening number of classes -- probably best to create a sub-package just for the event listeners (?).
I sort of see it like this:
* Model = Data/SQL, Filesystem etc (with accessors, setters and mutators)
* View = A tree of JComponent objects
* Controller = EventListener objects with some control over Model/View
Posted: Sun Oct 22, 2006 10:21 am
by Chris Corbyn
I totally forgot I have access to books24x7.com

The "Definitive Guide to Swing, Java" describe it like this:
(Hopefully I'm allowed to show that brief image)

Posted: Sun Oct 22, 2006 3:33 pm
by timvw
It's ok, i've got access to books24x7 too

(thx to skillport) (and for the access to skillport i have to thank compuware)... A chain of reponsibility

Posted: Fri Nov 17, 2006 9:00 am
by Case-
d11wtq wrote:I totally forgot I have access to books24x7.com

The "Definitive Guide to Swing, Java" describe it like this:
(Hopefully I'm allowed to show that brief image)

The picture is supposed to represent MVC?
If it does, I think it's completely wrong

.
Controller supposed to be in the middle, and model and view shouldn't have any knowledge of each other.
The idea of MVC is that you can reuse view component with other models and vice versa, just changing the controller.
So the controller handles the information between model and view.
Hope this helps

.
Posted: Fri Nov 17, 2006 10:43 am
by Chris Corbyn
I ignored that diagram... To be honest I could make head-nor-tail of it. I just took the obvious route of:
Components = View
EventListeners = Controller
Model (obvious)