Login Demo

Also available as a screencast.

Creating a Login Window

A login window is very simple GUI to design. We have the following basic requirements: In Swing, we will also want:

Thankfully, you will get the above for free when using either GridBagLayout or JGoodies' FormLayout. By the time we are done, we will have a GUI that looks like this:

Login Window on the Mac

First version

For the first version, we'll create the simplest thing possible. We'll be creating three files:

The View

We'll start by creating the view first. Right-click on the source package you want to work in and select "New -> Java Form"

New Java Form

The form creation dialog will open. You will need to enter a class and factory method name. At this stage, we're not going to worry about input parameters. We'll call this class LoginView and the method create. The form:

LoginView create form

Click "Create" to get started. For the view, we're going to need a few JLabels, a JTextField for the username, a JPasswordField for the password, a "Cancel" button for nervous users and a "Login" button to finish it off. You can drag these items from the palette at the bottom of the screen. Drag everything onto the design and then start aligning the components up.

LoginView step 1

By default, the frame's contentPane will be set to GridBagLayout. GridBagLayout is a very powerful, but incredibly difficult to use layout manager. GUIDE exposes the layout as freeform design, continuously inferring the optimum constraints from the position of the components. Notice also how components will snap to guides when they line-up or approach other components. This snapping makes it very easy to set up correct layout behaviour. Another thing to note, is that when you align the JTextField with a JLabel, you'll see a guide representing baseline alignment. We'll use these features to create a nice looking view.

Showing baseline alignment

Once we have everything aligned up, we can start editing the properties. You can edit the text of JButtons and JLabels directly by double-clicking on them.

Editing a Label

That's mostly it for the view. You can test your view using the design preview function. From the menu, select "Design -> Preview" to test your view. In the design preview, you can test the view's resize behaviour, how it looks in different look-and-feels, and how it will respond to the pack() method.

At this stage, you may want to have a look at the file GUIDE has generated. As we have chosen the "New -> Java Form" from the menu, GUIDE has generated a factory method that generates a JFrame. It should be fairly obvious how to use this, although at this stage our GUI doesn't do anything useful.

The Controller

Now that we have created a view, we need to create the Controller to respond to user actions. In GUIDE, a controller must satisfy the following:

The most obvious place to start to with our controller, is the login event; or what to do when the user clicks "Login". To do this we create an action on our controller. An action is just a public method that takes no arguments. Now our controller looks something like this:

Simple Controller

This doesn't do anything useful yet, but we can still get started by linking the button to this method. But first, we need to let GUIDE know about our new controller. Open up the view in your editor and change the signature of the factory method to take our controller as an argument.

Adding Controller to View

If we return to GUIDE, it will detect the file change and load your controller. If you click on the "Actions" tab of inputs, you should see this:

Actions Tab of controller

Our method, or action, now shows up. To create an EventHandler that responds to button clicks, simply drag the action from the "Actions" tab onto the login button. If you return to your editor, you will see that GUIDE has create java.beans.EventHandler that calls the login method in response to an "actionEvent". You can test this out immediately using "Preview" from the design menu. Note that when you click the "Login" button, the "Message" pane of the Preview Controller logs the event.

Preview Simple Controller

Now we need to actually do something useful in the login method. Nominally, the login method will need to valid the input of both the JTextField and JPassword field. Clearly, it will need a reference to these two components. If we just create simple setters for these components on our controller, GUIDE can inject the values from the view. Our new controller now looks like this:

Simple Controller with injection

When we return to GUIDE, it will pick up the changes and if you click on the "Properties" tab of inputs you will see the two setters. To tell GUIDE what to inject, Alt-drag (or CMD-drag on the Mac) the JTextfield onto the userNameField setter, and the JPasswordField onto the passwordField setter. It's time to test our GUI as a standalone program

Putting it all together

The thing we need to do is put this all together in the form of a program. Our program would look something like this:

Login Demo Program

That's it. There's more to be done, but it's more of the same type of thing. Make sure to look at the finished controller in the Sample Project.