MVC : Can View update/change Model

I’ve been reading about the MVC paradigm lately, trying to get a better understanding on how Swing works. There’s only one thing that confuses me.

I thought that only the Controller (~ Listeners?) was allowed to update the Model, so that the View can update its visual representation. Now, i’ve been working with a JTable (~ View?) and i saw that it has a method setValueAt(). When i took a look at the source code, i noticed that this method directly updates the model. The guys at Sun probably know what they are doing :), so i probably am missing something. But what?

M.

I’ve been reading about the MVC paradigm lately, trying to get a better understanding on how Swing works. There’s only one thing that confuses me. I thought that only the Controller (~ Listeners?) was allowed to update the Model, so that the View can update its visual representation.
It’s my understanding that because of practical problems, Swing does not implement the MVC paradigm. The View and Controller are represented by a single composite object that corresponds to a view, and the MVC model degenerates into a document/view architecture, which Sun calls the Separable Model architecture.

             ______________________________
            |                              |
            |            View              |
            | (the look of the component ) |
            |  /          /\\               |
            | /            |               |
Model <=====|/             |               |
(component) |\\             |               |
            | \\           \\/               |
            |  \\       Controller          |
            |  (Deals with events making   |
            |   changes to the model or    |
            |   view)                      |
             -------------------------------

Document/view architecture: document = the data, view = how the data is displayed.

Actually, Swing follows MVC very well.

Just be cause a component has aspects of being the view and the controller, doesn’t mean that that component doesn’t follow MVC.

For example.

The browser you’re using is a component.

It uses a Model (the html that is downloaded from the web server.)

It has a View (the part where we read the downloaded html.)

It has a controller (the scroll bar or even the selection parts that lets you set the size larger on the screen or not…)

So, you have one component that mixes the View and the Controller together.

JTable is the exact same.

It uses a model, it has a view, and it has a controller. Each piece is separate and different. Separate, they aren’t very good, but when you start to mix them together to make a component, that is when the power of MVC starts to come together.

Regards,

Nate

In the MVC diagram I am looking at, the View cannot update the Model. I think that was what the poster was asking about: i.e. how come in Swing the View can update the Model. I think the answer is: Swing does not use MVC, it uses something else. If you still want to call it MVC, that is up to you.

Not to be too nit-picky. But which view component changes a models value? Are you sure that there isn’t a quick change to a controller at a specific point that might look like a viewer that is changing the model’s value?

Regards,

Nate

Exactly Nate … and lets take your scroll bar example. Does the scrollbar itself effect any change? No, its ActionListener does … so in some way there is seperation.

All in all there are times when complete seperation of model / view / controller is extremely difficult, sometimes near impossible without some really “over done” code…

Funny, I just started ready this J2EE without EJBs and I have to agree with some of what he says… some of this is overly complicated for no apparent reason…

Not to be too nit-picky. But which view component changes a models value? Are you sure that there isn’t a quick change to a controller at a specific point that might look like a viewer that is changing the model’s value?

I would argue it doesn’t matter. If the diagrams I am looking at are correct, Swing employs 2 objects. One object handles the view as well as providing some additional functionality. The other object is the Model which contains the raw data. The object with the view has the ability to change the raw data (Model). Can you divide an object up and say this part of the object is really the View and this part of the object is really the Controller, and therefore the structure represents an MVC architecture. If you are allowed to do that, then is this an example of MVC architecture:

class  MVC
{
	private int number; //[COLOR=Red]MODEL[/COLOR]

	public MVC(int number)
	{
		this.number = number;
	}

	public void view()  //[COLOR=Red]VIEW[/COLOR]
	{
		System.out.println("number :" + number);
	}

	public void controller(int newNumber)  //[COLOR=Red]CONTROLLER[/COLOR]
	{
		this.number = newNumber;
	}
		
}

class  DemoMVCArchitecture
{
	public static void main(String[] args)
	{
		MVC myModel = new MVC(10);
		myModel.view();
		
		myModel.controller(100);
		myModel.view();
		
	}
}

??

Ney, my brethren. That is not MVC and neither is a two object model. MVC demands three distinct Model, View, Controller objects!! What evil doers go there and falsely covet the MVC name and make false promises thereupon!!!

Hey, now.

Maybe this will help you?

The designers of the Swing user interface components of the Java Foundation Classes (JFC) designed each component around a pattern called Model/View/Controller (MVC). The result is a user interface toolkit of almost unmatched flexibility. This month we’ll explore Swing’s underpinnings and build our own user interface components.

Taken from ‘MVC meets Swing’;

http://www.javaworld.com/javaworld/jw-04-1998/jw-04-howto.html

…and from the same article:

As I mentioned a moment ago, the MVC design pattern separates a software component into three distinct pieces: a model, a view, and a controller.