• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MVC in swing

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alex English
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Alex English
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,
That makes a whole lot more sense...

Thanks so much for your help with this.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic