Welcome to Dream.In.Code
Become a Java Expert!

Join 150,100 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,854 people online right now. Registration is fast and FREE... Join Now!




Model View Controller with Java Swing

 
Reply to this topicStart new topic

Model View Controller with Java Swing, Design question

dreamerGon
24 Jun, 2008 - 10:00 PM
Post #1

New D.I.C Head
*

Joined: 26 May, 2008
Posts: 2

Hi, I'm not sure if this is supposed to be in the Java section or Software Development section since MVC isn't only a Java thing, so sorry if this is in the wrong place.

Anyway, I'm trying to create an application strictly adhering to the MVC paradigm. I think I've got the model part down, but I'm having a little trouble deciding what goes where when it comes to the View/Controller part. I have a bunch of Swing components in the View, and a bunch of ActionListener subclasses in the Controller that update the model when something happens in the View such as a button click.

Getting to the point, my question is, what is the best way to add the ActionListeners in the Controller to the View? I have 2 ways in mind, but both have their drawbacks.

Method 1: Create "addActionListener" methods in the View for every component. Have the Controller hold a View reference and add ActionListener instances to each View component.
Example (In Controller): view.addButtonListener(new ButtonListener());
Cons: This gets really tedious since I have a lot of View components and gets even more annoying when the View holds subpanels each with their own components.

Method 2: Create "getActionListener" methods for each Listener in the Controller component. Have the View hold a Controller reference and use the get method from the Controller to get the appropriate Listener to add to the components.
Example (In View): button.addActionListener(controller.getButtonListener());
Pros: Adding the Listeners is a little more modularized - it can be done in each subpanel class.
Cons: I feel like this isn't really good practice because the View shouldn't hold a Controller reference right? From what I read it should be independent.

Apologies for the long-winded post, and any additional insights to the MVC paradigm would be much appreciated.

- DreamerGon
User is offlineProfile CardPM
+Quote Post

pbl
RE: Model View Controller With Java Swing
25 Jun, 2008 - 03:55 AM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
I think it is really personal choice but I will opt for method 1 because it will keep together the code of the listener with the component that fires it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:32AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month