I wish to learn how to apply MVC architecture to my Java project; mainly my work in Swing. Now there is no easy explanation or example how to write proper code using MVC in mind apart from these two I found here:

  1. http://leepoint.net/notes-java/GUI/structure/40mvc.html
  2. http://www.oracle.com/technetwork/articles/javase/mvc-136693.html

It seems to me there is not a defined MVC architecture in Java. From what I looked at it's what Java offers you at the base level of Wwing and what you can apply on your own is in the first tutorial.

Apart from the two resources above, can someone provide me with a source that can ease you in to Java MVC? A video tutorial maybe?


Solution 1:

Here is pretty nice example http://www.leepoint.net/GUI/structure/40mvc.html I tried to find simplest possible to let you feel the idea.

Anyway I don't like the:

Seems to me there is not a defined MVC architecture in Java.

You can use MVC, MVP and whole other patterns in various different languages. Java has nothing to do with MVC, except MVC design can be coded in Java. There are, however, some frameworks that have embedded MVC architecture and forces using it. Spring MVC is the most known - try to find more about it, maybe some tutorials.

Solution 2:

Model - Model represents an object or JAVA POJO carrying data. It can also have logic to update controller if its data changes.

View - View represents the visualization of the data that model contains.

Controller - Controller acts on both Model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps View and Model separate.

mvc simple example

Solution 3:

see this example mvc simple example which is adding two numbers program using swings.by this example you can easily understud the concept of mvc

Solution 4:

Take a look at the spring framework, you can use it also with swing as shown in this tutorial .

Solution 5:

Using Javas in-built capabilities of the so called Observer and Observable classes the following tutorial explains the concept of MVC by an example.

MVC by Example with observer and observeable classes