Download NetBeans!

20070427 Friday April 27, 2007

Building a Login Screen for a NetBeans Platform Application (Part 2)

Let's go a step further than yesterday's Login screen. Yesterday, I didn't say that the buttons in the LoginForm were not part of the LoginForm itself. Instead, they were provided by a very handy NetBeans API class called NotifyDescriptor. Or, in fact, they came from one of that class's children, called NotifyDescriptor.Confirmation. These NotifyDescriptor subclasses (and there are others) provide descriptions of notifications sent to the user. Basically, they're comparable to variations of JOptionPane, just with a lot more versatility.

The statement used to define the NotifyDescriptor yesterday was as follows:

NotifyDescriptor nd = new NotifyDescriptor.Confirmation(form,"Login");

This returned a JPanel called form:

However, the JPanel looks like this in the GUI Builder:

Hence, notice, no buttons. So where do "Yes", "No", and "Cancel" come from? Answer: They are the default buttons that you get when you use NotifyDescriptor.Confirmation. Now here's the problem—how do you listen for actions on these buttons? When "No" is clicked, for example, something different should happen to when "Yes" is clicked. Presumably. How does one listen to those actions, when they relate to buttons that you didn't define yourself? Answer: NotifyDescriptor.setOptions.

Using that method, you can define your own buttons, so that you can listen to their actions. So, in the installer created in yesterday's blog entry, I now have two buttons:

JButton ok = new JButton();
ok.setText("OK");

JButton cancel = new JButton();
cancel.setText("Cancel");

Here's the action listener on the Cancel button:

cancel.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        LifecycleManager.getDefault().exit();
    }
});

(See the NetBeans API LifecycleManager for details.)

And here, to show you some basic interaction with the LoginForm, is the action listener on the OK button:

ok.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        String userName = form.getUserName();
        String password = form.getPassword();
        JOptionPane.showMessageDialog(null,"UserName: " + userName + 
                "\nPassword: " + password);
    }
});

And now... we simply add our buttons to the NotifyDescriptor, via the aforementioned setOptions method:

nd.setOptions(new Object[] {ok, cancel});

The final step is the same as yesterday, we display the dialog, using our notify descriptor implementation:

DialogDisplayer.getDefault().notifyLater(nd);

And that's all. Now I have control over the buttons, because I created them myself, overriding the three buttons I got for free from the NetBeans API class:

And this is the basis for creating your own authentication mechanism, i.e., a login screen.

Click here to go to Part 3 of this series.

In other news. While working through the above, I learned two interesting things about NetBeans IDE 6.0. Firstly, when you press Alt-Insert over a field, you get a very cool little pop-up leading to some cool code generation functionality:

And, secondly, I'm one of those people who always forgets which slash to use for a "forward slash" and which slash to use for a "backward slash". However, it looks like 6.0 will come to my rescue. Look at the difference between "\n" and "/n" in the following two screenshots and you'll see why I'll never screw this part of my code up again:

So, because the "\n" is shown in bold, I know that that combination is a line break, while "/n" will be interpreted as normal characters. Useful, isn't it?

Apr 27 2007, 04:18:40 AM PDT Permalink