Welcome to my MVC Java Tutorial. I have been asked for this tutorial many times in the last few weeks.

To understand the Model View Controller you just need to know that it separates the Calculations and Data from the interface. The Model is the class that contains the data and the methods needed to use the data. The View is the interface. The Controller coordinates interactions between the Model and View.

87 Responses to “MVC Java Tutorial”

They are technically going on right now. I’m going to make professional grade games which are going to require things like object oriented design and design patterns. When the proper game tutorials starts it will go on for at least 6 months. I hope you like them

Thanks for the very useful Videos! I have always wonder shouldn’t the model store ALL the data that is going to be displayed in view like first_number and second_number as well? Assume an example where the first_number is the score of a student out of total marks which is the 2nd number. The sample function is going to just calculate the % of the student in the test. Assume the view wants to print the whole report of student later, it could just ask the model for that information? What do you think? Thanks again!

There are many ways to implement an MVC. Yes you are correct that model should contain all of the data, but when I am implementing a system I tend to decouple the model completely from the view if possible. I could have kept them decoupled and still store the numbers in the model with a FocusListener, but I was also trying to keep the code as uncomplicated as possible. I hope that clears some things up. You made a good point 🙂

Thank you so much for this awesome tutorial. I was wondering if you could clarify the MVC design pattern with regards to Servlets, JSPs and Java classes. my main issue is the communication between the Controller(Servlet) and the view(JSP). and is it the best way to implement MVC if you have your model create certain parts of the view EG:a Table, then pass it on as a String value to the Controller which in-turn sends it to the view to display.To communicate(Controller and VIEW) I’m currently using JQUERY,AJAX AND JSON

I’ll cover JEE concepts as soon as possible. I go out of my way to avoid having the model do anything but handle data and process data. I try to have the controller handle all communications between the Model and View.

This isn’t the only MVC pattern by any means though. Many allow the model and view to interact. If the code is understandable I have no problem with that.

Thank you very much 🙂 The controller would just work with both of them. Not much would change here. It is all about the action listeners and triggering the right methods when needed. Sorry, I can’t really say anything else because I’m not sure what may be hanging you up

I’m from Queretaro Mexico, dude I really love your site, your work is just amazing.

I’ve a question, hopefully you can guide me, for example, if I have two or more buttons in my main window and each one has a different action, should I create one inner class in my controller for each different button ? Is that the right way to do it ?

Basically you need to define what method to call in the controller when the button is clicked in the view like this

// If the calculateButton is clicked execute a method
// in the Controller named actionPerformed
void addCalculateListener(ActionListener listenForCalcButton){
calculateButton.addActionListener(listenForCalcButton);
}

Awesome tutorial! I’ve been out of the programming realm for a while now, but I want back in, and recently I saw MVC as preferred knowledge for a job opening. Google got me to your tutorial, and I must say I have a great understanding of what’s involved in MVC. Now I just have to reinvent my programming methods, which are pretty rusty and outdated anyway.

To tell the truth though, I don’t understand 100% why the view and controller are separate…

Thank you 🙂 A major step for a programmer is investing some time into understanding object oriented programming completely. That is probably what you are struggling with? The whole idea behind MVC is to separate the components so that they can be changed without breaking, or even effecting the rest of the system. Just like how we can put new windshield whippers on our car with worrying about the whole car breaking.

Awesome tutorial. I just started learning MVC and I have couple of questions:

1) So, the whole concept of MVC is to break down the code into seperate programs to make coding more clear, is that true? Meaning, instead of writing 1 program with thousand lines of code, we break down into 3 different programs (1 for model, 1 for view, and for controller). Is that correct?

2) I want to practice using Eclipse and copy your code and play with it. Can you tell me what I need to do? Do I download Eclipse? How do I set it up? What else do I need to download in order to follow your Calculator code?

3) Can you give me another tutorial like this but using SQL database? Like an address book scenario. Can you please explain to me?

1. MVC is used for many reasons. It helps break pieces of your system into real world objects. It also makes it easy for you to change the model and view without breaking your program because the controller handles communications between the two.

I find your tutorial very useful. I have created a view made up of three input components. Two of these provide the filling for sql stored procedures executed by the controller.

I have created a model for each of these components. There is only one controller. Is this the best way to do it?

What is the best way of identifying which button was clicked, there are three. Would you use the onFormEvent or just use an action event on the buttons involved. How would you update the model and view from a controller that ran a sql stored procedure.

Hello Derek,
Thanks for the wonderful tutorial.
I have a question: How exactly is this different from MVP, Model- View-Presenter? I thought that in MVC the model updates the view, whereas in MVP the controller liaises between the View and the Model (which is what you’re doing in this tutorial).

The MVC pattern shown here demonstrates changing the model data by interacting with the view which is fine, but how would one change the model data by having the two numbers initially stored in a file and then reading those numbers so that the Model and View are updated? Lets say we have defined class that reads the file and stores the two numbers in instance variables of that class. How does that class fit into the MVC pattern?

Thank you 🙂 That is why the MVC is so brilliant. If you need to add a feature and the user asks for that feature everything changes accordingly, but they still don’t need to know how the task is performed.

1. The view updates to allow the new feature if it must.
2. The controller catches the request from the view and passes it to the model.
3. The model performs the needed calculations and then sends them to the controller.
4. The controller provides an update to the view.
5. Anything can change and as long as we can agree on a solid way to communicate we don’t lose flexibility.

Hey Derek,
If we try to implement this Calculator in Business Delegate along with MVC(in single code), so could you tell me ‘who will do what?’
I mean
Business Delegate’ service = MVC’s Model
but what component of Business delegate will play role of controller
and similarly where we can put VIEW component in business delegate…
thank you so much in advance!!!

You may want to take a look at my Android tutorials because everything is based on MVC. You basically have one controller that handles all communication between the views and the models. If the view needs to change it tells the controller and it serves up a new view. If a new calculation is needed in the model the class is added and the controller just needs a way to communicate with that new class.

hey dude i saw your many tutorials and they are great
and i also decided to run YouTube channel for java programming
but i don’t know where to start ,technically i ran out of
ideas for it because there are lot of tutorials in YouTube .
so if you have any suggestion about where to kickoff it will helpful to me
well you can also suggest some software development books on java
thanks and keep making tutorials they are really fun to watch

The Java book that everyone seems to like is the Head First Java Book. I know a lot of people want Java enterprise tutorials and I’ve never made one. I think NetTuts has one you could use as a guide.

If you make a lot of videos eventually you’ll find your way. It took me about 2 years to get comfortable with my style. Just make tutorials that you enjoy and you’ll do great. I wish you all the best 🙂

Can anyone help me? I’m new to Java as well as MVC. I made the program work by following the tutorial, but is there any more models for MVC instead of a Calculator model? or does it depend on what we put inside the java to make it look and work like a calculator?

Thank you for this tutorial!! It definitely cleared things up about the basic structure of the MVC model.

I do have a question, though: You mentioned in a response to another commenter that a project could have many models and views but should only have one controller. Could you explain or demo how to have this program call and display the solution in a different view when the user clicks ‘Calculate’?

I’m trying to understand how the controller gets any other view or model than the ones you passed to it from your main class.

You’re very welcome 🙂 You could definitely have multiple MVCs in one project. It is very common though for a controller to implement an ActionListener and contain a List of ActionListeners to maintain multiple models and views for example.