wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes MVC in swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "MVC in swing" Watch "MVC in swing" New topic
Author

MVC in swing

Alex English
Greenhorn

Joined: Feb 26, 2007
Posts: 8
Hi all,

I am new to swing, coming from web-based applications. I am attempting to use an MVC-type architecture for a small utility that I am writing.

Can you tell me if I'm on the right track?

I can have the view, which is all the swing components etc


Does that kind of architecture make sense? Of course i have seen things like that handles setting up the GUI and handling events, but it seems like if the GUI were to ever get big, i would be in trouble.

How do people usually do MVC in a swing application?
[ February 26, 2007: Message edited by: Alex English ]

-A
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
In general I agree that too much behaviour in the GUI layer can be a real problem, not just for maintainance as it gets bigger, but for testing even when it is small.

My main worry about your code, though, is the circular reference. Each class needs to be aware of the other when it is compiled, and this can cause some strange behaviour. I suggest that at least one of the two refer to the other through an interface.

I'm also not sure about constructing the controller in the view. Some how that seems backward to me. One of the benefits of a MVC appeoach is that each of the three elements may actually be multiple instances. This seems especially common with the view element (two explorer windows on the same folder, two browser windows on the same URL, viewing the same data as a table and a graph, and so on.

When I do this sort of thing, I typically create the controller(s) before the view(s) and pass a reference to a created controller into each view when it is created. Typically, both controllers and views need to be passed a reference to a pre-created model. too.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Alex English
Greenhorn

Joined: Feb 26, 2007
Posts: 8
Yes, the circular reference is what made me question the whole thing to begin with.

So I could create a thinner ActionListener, which really just passes arguments to the controller?



But isn't the purpose of a controller in a setting like this to be able to manipulate the components of the GUI? Like changing the values in the text fields etc. Wouldn't the controller need a reference to the view in order to do that?
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356
hello,

I recommend to use an Action, possibly implemented by extending AbstractAction. Those Actions can be used as parts of a JMenu, a JButton and other things.

Sometimes the controller needs something about the view, as you noticed. For example a view that is an input form for customer data holds the information that the StoreAction needs. Especially in such a case do what Frank suggested, and use an interface that fits to the abstraction. For example the StoreAction might reference a CustomerFactory (interface type) which has a method public Customer createCustomer that retrieves the information from those form fields. It should not reference MyView, which is far more than it needs, and thus would mean close coupling. (MyView could implement CustomerFactory or it could not; it is irrelevant here who implements it and where it comes from.)

This is all a bit dirty, but a working compromise between using what Swing provides, simplicity and good design.

Kai


Kai Witte's business website Kai Witte's private homepage
Alex English
Greenhorn

Joined: Feb 26, 2007
Posts: 8
Hi Kai,

Thanks for taking the time to respond to my question. I think I see what you mean. The view should implement an interface which will only give up the information that the controller needs.

When the GUI starts to grow in size, each of the subcontainers can implement an interface which will give up useful information to the controller(s). Or even a button group or something like that.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
But isn't the purpose of a controller in a setting like this to be able to manipulate the components of the GUI? Like changing the values in the text fields etc. Wouldn't the controller need a reference to the view in order to do that?

Sure, but that's why I recommended having the Controller create the View rather than the other way around. Modifying your original code might give something more like:



That way there is no circular reference, and the controller can happily access view fields as needed.
Alex English
Greenhorn

Joined: Feb 26, 2007
Posts: 8
Ok,
That makes a whole lot more sense...

Thanks so much for your help with this.
Thiago Prado
Greenhorn

Joined: Feb 28, 2007
Posts: 1
I think this can help...

As a general rule:

The model does not know the view neither the controller.
The view knows the model, but it does not know the controller.
And the controller knows both the model and the view.

[]s
thiago.prado();
 
Don't get me started about those stupid light bulbs.
 
subject: MVC in swing
 
Similar Threads
MVC
naive questions about mvc
What is the best way to create GUI components- in-line, methods, inner classes or separate classes
How to achieve the ability of future functionality enhancements of user interface?
MVC