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?
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.
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)
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
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.