Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC in Java

This is about a school assignment so I'm trying to do things by the book. I feel like I'm getting the grips of Java, but good programming practice, design patterns, etc. are all rather new to me.

I've made my model and it works fine. It contains a student class which contains a number of fields with student information (obviously). Now I want this information to be displayed in a couple of JLabels. It is easiest to pass a whole student object to the GUI and use JLabel.settext.getname() etc. a number of times and no doubt this will work perfectly. But I feel that the student object is part of the model and by passing it to the GUI I'm not using a MVC pattern anymore. Am I right here?

I did make a controller object for passing data to and from the model and the GUI but for passing only strings to the labels or setting the JLabel text through the controller, I need either a lot of setters in the GUI, or I would have to make all JLabels global fields which doesn't feel good either.

Any advice on this?

like image 216
Berend Vervelde Avatar asked Dec 27 '08 10:12

Berend Vervelde


People also ask

What is MVC used for?

MVC (Model-View-Controller) is a pattern in software design commonly used to implement user interfaces, data, and controlling logic. It emphasizes a separation between the software's business logic and display. This "separation of concerns" provides for a better division of labor and improved maintenance.

Does Java have MVC?

The Model-View-Controller (MVC) is a well-known design pattern in the web development field. It is way to organize our code. It specifies that a program or application shall consist of data model, presentation information and control information.

Is MVC front end or backend?

MVC provides front and back ends for the database, the user, and the data processing components. The separation of software systems into front and back ends simplifies development and separates maintenance.

What is MVC in UI?

Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted from the user.


2 Answers

Note that the Swing components all use the MVC pattern internally, so they already have a model. This is more relevant with complex widgets like JTable, where you definitely want your model to implement the TableModel interface.

The big question is how to reconcile your domain model with the internal models of the individual Swing components. One way to do this is to have setModel() and getModel() methods in your GUI class, which translate between them, i.e. setModel() takes your model and calls setText() on the individual JLabels, etc.

like image 196
Michael Borgwardt Avatar answered Sep 17 '22 23:09

Michael Borgwardt


the GUI should worry about all the interface stuff. I guess you have a class that is your GUI for doing 'stuff' to the student with your JLabels. Just pass your student instance to this class and let it do what it needs to do. When it is done it will call a controller method to do whatever needs to be done.

OOD deals with passing the objects around that you want to manipulate. You don't need to break the objects apart for passing in MVC. You are supposed to pass that around really if this is a general case. The model defines the data objects that you will be working with... or more specifically the system will be working with (controller) and the users will be working with (GUI). These class are built to be passed around. You will have to do a lot more work if you un-encapsulate all the information hehe :)

like image 20
Arthur Thomas Avatar answered Sep 21 '22 23:09

Arthur Thomas