Implementing MVC with Swing (Java) ?

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Implementing MVC with Swing (Java) ?

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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)
Image
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 :P
User avatar
Case-
Forum Newbie
Posts: 22
Joined: Fri Aug 04, 2006 5:52 am
Location: Finland

Post 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)
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 :).
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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)
Post Reply