• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MVC in Swing

 
Carlos Obregon
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have used Java for a while but I'm not sure how to enforce MVC in a Swing application.

Normally I have a POJO's to represent the Model part and I have a main JFrame class which is also an ActionListener

My questions are: Do I create a single ActionListener to handle all events? Do I create an ActionListener that delegates based on the type of event? What happens if I also need a MouseListener?

Thanks!
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Carlos,

Using MVC in Swing requires some sort of registration for methods to be called upon when an event (action) takes place.

For example: you would want your action Listener methods to be located in the Controller class, and when the View notices an action (ex. button clicked) he (View) will call the appropriate method on the Controller. The controller accesses the model and possibly updating it in a way appropriate to the user's action, if the model has been updated, it notifies interested listeners of the change which is the Controller in this case.

You need to make sure that there is NO direct communication between the View and the Model, and all contacts go through the Controller.

You might want to implement Observer/Observable interfaces which allow you to register methods and notify parties when events take places.

Cheers!!!
 
Vish Shukla
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Take a look at HMVC pattern. It would be really maintainable and easy to use for Swing/ GWT applications.

HMVC Pattern
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic