aspose file tools*
The moose likes Beginning Java and the fly likes What is MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is MVC" Watch "What is MVC" New topic
Author

What is MVC

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
In MVC the model is the code that carries out some task.


http://csis.pace.edu/~bergin/mvc/mvcgui.html

But in other tutorials I read model is database and data.

What is model in MVC?

http://www.javaworld.com/javaworld/jw-04-1998/jw-04-howto.html?page=2

In the image in this site, I see that controller and view in UI, It means that controller is part of User interface?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The model is the logical component -- basically taking input, performing the processing, then returning output. A database might be part of this.

The view is the user interface.

The controller connects the view and the model -- basically by "listening" for user input from the view, then relaying that to the model for processing, and returning output to the view so the interface can be updated as appropriate. There are variations on this approach. As noted above the diagram in your link, combining the view and controller is "a common adaptation of the basic MVC pattern."

This example might help clarify: http://www.leepoint.net/notes-java/GUI/structure/40mvc.html


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
May you give me example how to use MVC in swing?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10384
    
    8

Standard Swing components are already "MVC enabled".
A JTable for example, replies on the underlying TableModel for the data. It replies on renderers/editors/listeners for user interaction.
Check http://download.oracle.com/javase/tutorial/uiswing/components/table.html for working code and examples


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
These components are classes, and are MVC enabled.

May you tell me how MVC is enable in Jtextarea? How MVC is built in this component?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Please show me the Model, View, Controller in JTextarea.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Swing components typically provide the View for your own MVC application. You provide the model yourself, and use the component to view your model. Control is usually achieved through adding listeners to the components.

For a JTextArea, you could say that the model is an instance of the Document interface. Most of the control is handled internally by the JTextArea, but you can add more listeners yourself if you want to implement special features. The view is the JTextArea instance itself, of course.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
instance of the Document interface


May you explain more about it?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Read the API for javax.swing.text.Document.

JTextArea uses some instance of Document to store the textual data. JTextArea itself only displays the data. Here, Document is the model and JTextArea is the view.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
According to the figure 2 in this article,

http://www.oracle.com/technetwork/articles/javase/mvc-136693.html

Only a variable can be considered as model. Right?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

No. A model can be anything that stores state information about your problem. Usually the model is a big collection of classes working together, and they provide some sort of public API through which to interact with the view and controller.

Note that 'model', 'view' and 'controller' are abstract concepts, and there are no hard and fast rules. It's just a helpful mechanism for thinking about your program.

Let's take a Chess game as an example. It could consist of ChessPiece and ChessBoard classes to keep track of the state of the game. There might even be another regulating class Chess, which may poll Player classes for their next move, and makes sure that no illegal moves are performed.

These classes together form the model of your chess game. Then, we could write a ChessPanel class, which has the responsibility of displaying the chess board and the pieces. This ChessPanel provides the view of your application.
Finally we could add MouseListeners, KeyListeners, etc. to the ChessPanel to make sure that we can interact with the game through our GUI. These listeners constitute the control part of the application.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Thanks,Good example
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
For a checkbox component, the data model could be a single Boolean variable, indicating whether it's checked or not. The behavior for handling mouse-click events would alter the model, and the view would examine that data when it draws the on-screen representation.


Learning JAVA , Orielly

Then only a variable can be a model!
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Then, I think that for JButton the string that is shown on Button is the model.Right?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
abalfazl hossein wrote:Then, I think that for JButton the string that is shown on Button is the model.Right?
Probably not. No. The text shown is only used by the user, not usually by the rest of the code, so I wouldn't call it the model myself. I am sure people will disagree, however.

The model would include things like whether the button is activated, what Listeners it has registered, what those Listeners do, etc. The displayed text, mnemonic, etc might be part of the model.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
JButton is in Swing package.Swing package is based on MVC.

What is actually the model in JButton?Which class?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Look through the documentation for JButton and use ctrl-f "model" and follow the links.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

It's not helpful to think of all Swing components as following the MVC pattern. Some components are just that: Simple components to help you build a GUI. There's nothing more to it than that.

MVC should be applied across your program more globally. You have a problem, you model it, and you add classes to display it.

If you really want, you could find an MVC pattern in the way a JButton solves the problem it was designed for; but it's not very useful to take patterns this far. Sometimes you just need to punch out simple procedural code.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Hmm, to further clarify my post; the fact that JButton indeed follows the MCV pattern shouldn't really matter to you. It was used by the designers to implement buttons, but in the end all that's interesting to the user (you) is that they have a simple button that can notify them when it's clicked.

MVC in Swing components is more interesting in classes such as JTable and JTree, where you can actually provide the model yourself and these classes display it for you.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
The Model is where the data for the component is stored. For example, in a JButton the button label is part of the button's model. In a JList, the list data is in the List Model. Ok.

An Example...

An example/analogy might help here, so let's use a JList and a computer. The JList has a model - in fact, it's called a ListModel. The ListModel is pretty much a Vector of the different strings you want to display in the JList. In the computer analogy, the model is the hard drive.


http://www.unknownroad.com/rtfm/swingtut/mvc.html

In this tutorial, The Model, is where the data stored.


I track the link that Campbell Ritchie pointed:

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/ButtonModel.html

State Model for buttons. This model is used for check boxes and radio buttons, which are special kinds of buttons, as well as for normal buttons. For check boxes and radio buttons, pressing the mouse selects the button. For normal buttons, pressing the mouse "arms" the button. Releasing the mouse over the button then initiates a button press, firing its action event. Releasing the mouse elsewhere disarms the button.


ButtonModel is just an interface, That is it.and an interface can not store data.Then How can it be a model?A model stores a data according to the tutorial, Like database like file like variable.Model is like a hard drive for a computer.


Dear Stephan, Are agree with that post that I told only a variable can be model?

For a checkbox component, the data model could be a single Boolean variable, indicating whether it's checked or not. The behavior for handling mouse-click events would alter the model, and the view would examine that data when it draws the on-screen representation.


** Is business logic equal to controller?

Thanks in advance, All friends!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

abalfazl hossein wrote:ButtonModel is just an interface, That is it.and an interface can not store data.Then How can it be a model?A model stores a data according to the tutorial, Like database like file like variable.


Do you know how polymorphism works? JButton uses *some class* that implements ButtonModel. The designers don't care what class this is, just so long as it implements ButtonModel.

Dear Stephan, Are agree with that post that I told only a variable can be model?


Often, a single variable is used to store a reference to some class that exposes the model API, sure. In JButton's case, this would be a ButtonModel. In my chess example, this would be a reference to an instance of Chess.
This is not necessarily always the case though.

** Is business logic equal to controller?


No, business logic is equal to the model. The controller just passes messages between your user interface and your business logic.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Models are not data access objects;


Agree?

Are methods that manipulate data part of model?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
abalfazl hossein wrote: . . . Models are not data access objects

Agree?
Probably
Are methods that manipulate data part of model?
Probably, but not certain.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
probably? May you tell more details?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
The term "Model" in regards to the Model-View-Controller object-oriented design pattern refers to a business object model. This model consists of one or more classes that are written to implement business logic. Along with business logic there is business data. Objects in object-oriented terms consists of behaviour and data, and there are typically many classes in a business object model design. Some of them may be all logic and some of them may be all data, and some of them may be a mixture of data and logic.

If something is said to be MVC compatible, or supports MVC, then this means that it can easily be used in the design of a MVC-based application. It does not mean that it implements all parts of MVC. For example, the Struts framework is advertised as a MVC framework, but it does not implement a "Model" and should not be used for anything in a "Model." Struts is only for building the View and Controller portions of a MVC app.

Hope this helps. Good luck!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
abalfazl hossein wrote:probably? May you tell more details?
No, because what you posted didn't have enough details to be sure. Also the boundaries between M and V and C may be indistinct.
 
wood burning stoves
 
subject: What is MVC