• Post Reply Bookmark Topic Watch Topic
  • New Topic

MVC design question with Swing  RSS feed

 
Peter Mohlen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I am new to Java an try to build some small Applications. Currently I try to understand the MVC.
As there are a lot of MVC "variations": I try to understand the MVC as described by Eric Freeman in Java Design Patterns Head First.

The example he provides is simple: Its model is one object (which uses only primitive attribute data types), and the View (which is notified by model if changes happen (so the observer pattern is used)) gets the data by simple getName,... methods. If the view changes a value, it calls the controller which notifies the model to change a value. The model notifies it's observers and the data is reloaded.

I tried to use a similar approach for the following application:
My model is a cinema which has a list of movies. The movies should be displayed in a (editable) JTable.

I started programming this app. In the View I created a TableModel which gets the movie list from the model and pushes it to the JTable. Suddenly I recognized: I never notify the model to update something. As I get a reference to the movie list from the model, the model is always up to date.

My question is: Is my desgin right?
How would you solve such a problem?
What is, if I have a "Add movie view" - Should I notify the controller if the "Add movie button" is press which notifies the model and than reload the whole moviedata in the view from the model?
Is it allowed to give the view an object or just simple primitives?

I am happy if somebody could give me feedback and I hope I am allowed to reask something, if I don't understand your reply !

Kind regards
 
Peter Mohlen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I have to make my question more concrete:

I am using the classical model view controller pattern. In the View I have an editable table which represents the data of the view. Addionally, there are textfields, buttons,... to create new objects.
Where should I create the table model? In the view or in the controller? Is there a general answer?

Currently, I would make the following thing: create the table model in the controller and push it to the view. If the add button is pressed, I call the controller, which calls a method in the model to create a new object. If a new object was creaed, the view is notified which refreshes the gui.

I am happy for any feedback
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The general idea with MVC is that you should be able to disconnect the view and you still have something that "works". That is, no business logic in the view.

Sometime models have all the business logic. This is the way I like it. Sometimes there is business logic in the controller.

Business logic: decisions that have to do with the way your project works, not how to display the view or where you go after pushing a button.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it allowed to give the view an object or just simple primitives?

An object is fine. The View is only concerned with the UI so it might display a List.

The controller is concerned with the controlling the UI. It also passes data (can be objects) back and forth between the View and the Model.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!