Lab 1: Creating A Simple MVC App Using Java/Swing: 1. Opening The Existing Project
Lab 1: Creating A Simple MVC App Using Java/Swing: 1. Opening The Existing Project
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.
However, correct the generated code to include only the number of accounts in order to avoid too complicated
string representation:
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.
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:
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:
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.
define these three fields (do not forget to use our SecurityManager instead of java.lang.SecurityManager),
generate a constructor setting the SecurityManager
The creation of the View was coded in the main() method of LoginDialog.java. We modify this method:
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:
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.
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:
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: