0% found this document useful (0 votes)
82 views

Lab 1: Creating A Simple MVC App Using Java/Swing: 1. Opening The Existing Project

This document provides instructions for creating a simple login functionality for a desktop banking application using the MVC pattern in Java. Key steps include: 1. Creating interfaces and classes for the model (LoginInfo), view (LoginDialog), and controller (LoginController). 2. The controller connects the model and view, and handles login button clicks by calling the business logic in SecurityManager and updating the view. 3. Event handlers in the view sync data between the view and model. The controller handles exceptions and displays success/failure messages.

Uploaded by

Daniel Horvath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Lab 1: Creating A Simple MVC App Using Java/Swing: 1. Opening The Existing Project

This document provides instructions for creating a simple login functionality for a desktop banking application using the MVC pattern in Java. Key steps include: 1. Creating interfaces and classes for the model (LoginInfo), view (LoginDialog), and controller (LoginController). 2. The controller connects the model and view, and handles login button clicks by calling the business logic in SecurityManager and updating the view. 3. Event handlers in the view sync data between the view and model. The controller handles exceptions and displays success/failure messages.

Uploaded by

Daniel Horvath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab 1: Creating a simple MVC App using Java/Swing

In this Lab you will be creating a simple MVC application using Java, Swing and NetBeans. During the semester our
example will be a simple online banking application. In this lab we shall start implementing a desktop client, and
create only the login functionality of the application.

You can find two NetBeans 7 projects in the labor material, BankDesktopStart is the starting point for your work,
while there is also a finished version BankDesktopFinal.

1. Opening the existing project


Open the BankDesktop application with NetBeans. You will be provided with the data access layer, namely with the
entities User, Account and AccountHistoryItem, the corresponding DAO interfaces and implementations, and a
factory/registry for the DAOs. Please, review all the given code that you will use during the Lab. You need not take
even a single look into the DAO implementations because you will use only the interfaces.

You get also a simple test program in BankDesktop.java.

2. Testing the data layer


Run the test program. It only loads the User entity with ID=1 and prints it.
The output is not very nice. Lets open User.java and let NetBeans generate a toString() method using the Insert
code local menu item (Shortcut Alt+Insert):

We can leave all the attributes of the class checked:

However, correct the generated code to include only the number of accounts in order to avoid too complicated
string representation:

The result is:


3. Coding the business logic layer
Let us continue with the business logic. For the login process we need only a single method checking that the given
username and password is correct or not. Create a SecurityManager interface in the manager package for this
purpose:

Include the only method declaration. Note that we omit the public keyword because all methods of an interface
are public by default:

Create the manager implementation class. When you declare that it implements SecurityManager, you will get
compile error:
Use the bulb on the left side margin to correct the failure, i.e. to complete the code with dummy method
implementations:

For the implementation we need a UserDao dependency. Introduce a field userDao and use the Insert code
(Shortcut Alt+Insert) to generate a constructor setting this field:

Now we can implement the authorize() method. We ask the userDao to load the user with the given username, and
then we check the password:

Note that you may need to press Ctrl+Space twice for code completion at the search of the referenced type User.

No its time to test our work. Let us open BankDesktop.java, and

introduce a field securityManager


initialize it in the initDependencies() method
complete the start() method with some test for correct and incorrect username/password.

Note that there is a SecurityManager in the java.lang package, so we must use the fully qualified class name
manager.SecurityManager, or we have to do the import declaration for it by hand, NetBeans will not do it for us.
Beside these two test cases think about anything that can fail, a programming mistake of us should be recovered
here! The solution can be found on the next page.
Hopefully you found out that we forgot to check not only the correctness of the password, but also the existence of
the user with the given username. The corrected version of the code is:

4. Implementing the View


Create a new Swing JDialog Form into the newly created view package. You can find JDialog in the Swing GUI Forms
category of the New File Wizard:

Use the Palette Tab to drag and drop GUI components into the JDialog according to the following screenshot. Use
Text Field and Password field for the username and password, respectively. If you cannot see the Palette, choose it
from the Window menu.
Use the local menu of the components in order to edit text and change variable names. We do not care about the
names of the labels but we rename the two textfields and the button:

5. Creating the model


We will create a class that can provide enough data for an initialization of the view and also for the event handlers in
the controller. At present we need only a username and a password field of type String. So

create a class LoginInfo in the newly created package model


add two String fields username and password
Use the Insert code wizard to generate getters/setters
Use the Insert code wizard to generate a constructor with username and password as arguments
create a no-argument constructor manually that calls simple this(,).

(We omit the getters/setter on the screenshot.)


6. Connecting the Model and the View
The Model will remain such a plain class, without any active code and references. On the contrary, the view will have
a reference to the model and will be able to copy data from the GUI to the model and vice versa.

Introduce the field model of type LoginInfo in the View and add the following two methods to it:

The first one is public because the Controller will later ask the View the refresh itself according to the view. It is also
called from the constructor of the JDialog. The second one is private, it will be used only in the View, namely in the
event handler of the button.

7. Creating the Controller


Create the class LoginController in the newly created package controller. The class needs to have reference to the
Model and the View, and will depend on a SecurityManager, because it delegates the evennt handlers job to the
business logic layer. Also

define these three fields (do not forget to use our SecurityManager instead of java.lang.SecurityManager),
generate a constructor setting the SecurityManager

8. Connecting the Controller with the Model and the View


The controller is responsible for creating and controlling the model and the view. A default Model can be
constructed in the Constructor of the controller:

The creation of the View was coded in the main() method of LoginDialog.java. We modify this method:

it will be called from the Controller


instead of the String[] parameter we pass the Controller and the Model
the Controller and the Model will be passed to the LoginDialog constructor, that will be set the
corresponding fields
the LoginDialog.create() method is not able to return a reference to itself, so the LoginDialog constructor will
set the Controller.view field ( a setter should be created in the Controller, it is not detailiert)

The changes in LoginDialog are highlighted with yellow:

/** Creates new form LoginDialog */


public LoginDialog(java.awt.Frame parent, boolean modal,
LoginController controller, LoginInfo loginInfo) {
super(parent, modal);
this.controller = controller;
this.model = controller.getModel();
controller.setView(this);
initComponents();
copyModelToView();
}

/**
* @param args the command line arguments
*/
public static void create(final LoginController loginController,
final LoginInfo loginInfo) {
/* Set the Nimbus look and feel */

public void run() {
LoginDialog dialog = new LoginDialog(new javax.swing.JFrame(), true,
loginController, loginInfo);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {


private LoginInfo model;
private LoginController controller;
// Variables declaration - do not modify

Note the last line of the constructor, it is the connection of the model and the view and was mentioned at the end of
Section 6.
The LoginDialog.start() will be called from a newly created method of the Controller:

9. Event handling in the View


The logic of the event handling will be coded in the Controller. However, a basic event handling code id necessary in
the View.

Switch to Design View, double click on the button to generate an event handler. You will be switched then to the
Source View:
When the user press the button, the data submitted on the view should be copied back to the model, and then an
event handler of the Controller should be called:

Let us click on the bulb and let NetBeans create the head of the event handler for us.

10. Event handling in the Controller


Then we code the logic of the event handler. The code follows on the next page, please try to follow the instructions,
and at the end check your solution

First note that every possible exception not caught in the controller would be shown to the end user that is not
that we want. So we put our event handling code into a try-catch block.

Next, we delegate the decision to the business logic layer, that is we call our only business method. The input for it
comes from the model.

Then, according to the success / failure / exception we show the result to the user. we implement a simple function
for it:

In the success branch we end our MVC demonstration with a System.exit(0) call. In a real situation the control should
be passed to the Controller of the next View (Main Page of the Bank App).

The failure and the exception branch could be almost the same except the message to the user and the printing of
the exceptions stacktrace to the standard error chanel. In this case we should remove the submitted password from
the Model and ask the View to refresh itself according to the model.
Note the commented out line. Of course, we should also close our view. Let us uncomment this line, and use the
bulb to create the missing method head in the View. The implementation will be as easy as follows:

11. Entry point


The last step is to code the entry point for our MVC demo, that is, to create the Controller in the BankDesktop class
und start it.

Let us rename our previous start() method to test(), use the bulb to generate a new start() method for us, and then
implement the new start() method according to the previous paragraph:
12. More initialization of the Model
Before testing our work we can complete the start() method of the Controller with some dummy Remind username
and password functionality as follows:

You might also like