954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Model View Controller with Java

Hi Guys,
I am quite experienced in programming with Java. I have though come accross a small problem, I don't know how to implement the MVC pattern. I understand the concepts of the MVC pattern and I was hoping that someone on this forum could help.

I have a model which is just a class that adds 2 integers together and returns the answer. The code is shown below.

=====================================================
public class Adder
{

public int add(int x, int y)
{
return x + y;
}
}

=====================================================

Now what I want to do is apply the MVC pattern to the above model. I want to have a Swing interface with 2 textboxes and an equals button which then displays the answer. How do I apply the MVC pattern?

Please don't try and mix the view and contoller in one - keep it separate.

Thanks

chopram
Newbie Poster
5 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

And is there a particular reason you need a separate controller and don't want the view to serve that role? If you don't have the answer then you probably don't need to. Keep the model separate and let the view interact with it as needed.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

I want a separate controller because I just want to see how its done. I'm guessing that in larger applications you need a separate controller & view? If you could just show me a sample I would be grateful. Thanks.

chopram
Newbie Poster
5 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Here's an article that describes the separation and interaction as it pertains to Swing apps:
http://www.javaworld.com/jw-04-1998/jw-04-howto.html

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This is even more extensive and perhaps more relevant to you:
http://java.sun.com/developer/technicalArticles/javase/mvc/

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

i still dont understand.

chopram
Newbie Poster
5 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

in that case you should go back to tha basics and try some more things on the level of "Hello World".

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You