Learn to make a MVC application with Swing and Java 8

The Model-View-Controller is a well known software architectural pattern ideal to implement user interfaces on computers by dividing an application intro three interconnected parts. Main goal of Model-View-Controller, also known as MVC, is to separate internal representations of an application from the ways information are presented to the user. Initially, MVC was designed for desktop GUI applications but it’s quickly become an extremely popular pattern for designing web applications too. In that tutorial, you’re going to learn how to create a MVC application with Swing and Java 8.

First, you need to know more details about MVC pattern. MVC pattern has the three following components :

  • Model that manages data, logic and rules of the application
  • View that is used to present data to user
  • Controller that accepts input from the user and converts it to commands for the Model or View.

Furthermore, the MVC pattern defines the interactions between these three components like you can see in the following figure :

Image for post
Image for post

The Model receives commands and data from the Controller. It stores these data and updates the View. The View lets to present data provided by the Model to the user. The Controller accepts inputs from the user and converts it to commands for the Model or the View.

Now, it’s time to start implementation in a real Java application. We’re going to create a simple MVC application that displays a form to the user letting him to enter his firstname and lastname and save them. Two other actions will be available : one to say Hello with firstname and lastname, and the second to quit the application.

1. Create the Model

Model is really simple. It’s a Java Bean with two properties : firstname and lastname.

2. Create the View

Now, we can create the View that uses Swing to make a Graphical User Interface. The View builds the GUI in its constructor and offers getters to all its visual components. It will be useful to add action listeners later in the Controller :

3. Create the Controller

The Controller has reference of the Model and the View. It needs and initController method to add action listeners to components of the GUI. To have a less verbose code, we use Lambdas added by Java 8 to add action listeners. Actions offered to user are implemented via specifc methods sayHello, sayGoodby, saveFirstname and saveLastname in the Controller :

4. Assemble our MVC Application

Now, it’s time to assemble all the three components of our MVC application :

Like you can see, it’s really simple.

5. Demo

Once your MVC application is launched, you have the following result :

Image for post
Image for post
Image for post
Image for post

6. Bonus

You can enjoy this tutorial in a Youtube video also :

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store