目录

我的学习分享

记录精彩的程序人生

Building a Login Screen for a NetBeans Platform Application

loginform1inrcp.png

Since the resolution of issue 92570, it is possible to show a modal dialog after the appearance of an application's splash screen, but before the appearance of an application's main window. And what would one want to put there? A login screen, of course. With this enhancement, it is possible to integrate some level of authentication into your NetBeans Platform application.

The specific method in question here is DialogDisplayer.notifyLater, which can now also be called before the main window is opened. When called from ModuleInstall.restored, the modal dialog is opened and blocks the main window until the modal dialog is closed. So, to implement this, use the Module Installer wizard, which will create a class that extends ModuleInstall (and adds relevant entries in the project.xml file and manifest file). You get a skeleton restored method for free. Fill out the method as follows:

public void restored() {  
   NotifyDescriptor nd = new NotifyDescriptor.Message("Ok");  
   DialogDisplayer.getDefault().notifyLater(nd);  
}

You will now get a simple modal dialog, after the splash screen, where you must click OK before the main window of the application can open. See further examples in the Javadoc for Class NotifyDescriptor.

However, you can also create your own JPanel and return that in the above code, instead of "Ok". So, here's my new code:

LoginForm form = new LoginForm();  
public void restored() {  
   NotifyDescriptor nd = new NotifyDescriptor.Confirmation(form,"Login");  
   DialogDisplayer.getDefault().notifyLater(nd);  
}

And your LoginForm could be anything here. Mine looks like this:

loginform1inrcp.png

It could be wired up to a database, with logic in the panel for verifying the password. So, the key to all of this is the fact that it is a modal dialog that appears after the splash screen and before the main window. Hurray!

Continue with part 2 of this series...


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:

loginform1inrcp.png

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

loginformnotifydescriptor.png

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:

loginform1inrcp.png

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:

generateconstructor.png

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:

forwardslash2.png

forwardslash1.png

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?


jxloginswinglabs1.png

I got a great tip from NetBeans Platform application developer Maksim Mednik the other day. Way back on April 26 and April 27, I wrote about the consequences of the resolution of issue 92570. In a nutshell, you can now show a dialog box before an application's main window opens. In other words, that's the spot where a login screen can be shown. Maxim took a next step in this scenario by... implementing the Authentication Framework provided by SwingLabs.

The result is pretty cool. Now, when the application starts, this dialog box is shown after the splash screen has done its thing, but before the application's main window is shown:

jxloginswinglabs1.png

Assuming you fill in the correct user name and password, the login screen vanishes and the main window of the application appears. As one would expect. But, what aboout those days when you wake up thinking you're Henry VII, King of England? Well, on those days you can't get very far:

jxloginswinglabs2.png

Quick run through for how to set everything up, assuming you already have an application (otherwise there's not much point in having a login screen, is there) together with Maksim's helpful code:

  1. Create a library wrapper module project for the SwingLabs JAR.

  2. In the module where your Login screen is going to be, set a dependency on the library wrapper module.

  3. Use the Module Installer wizard to create stubs for a module installer. (The wizard also adds the correct dependency on the related NetBeans API module and adds a line to the manifest file.)

  4. Replace the content of the installer with this file.

  5. Create a new Java class, for the panel. Replace the content with this file.

  6. Run the application.

Maksim ends his e-mail to me with the words: "It took me a while to figure out how to call exit when dialog is closed. I didn't find any nice way of calling setDefaultCloseOperation and I'm not sure where one can get a result value from DialogDescriptor when notifyLater is called. Any thoughts would be highly appreciated."

If anyone can help with this question, please leave a comment at the end of this blog entry.


https://blogs.oracle.com/geertjan/building-a-login-screen-for-a-netbeans-platform-application-part-1
https://blogs.oracle.com/geertjan/building-a-login-screen-for-a-netbeans-platform-application-part-2
https://blogs.oracle.com/geertjan/building-a-login-screen-for-a-netbeans-platform-application-part-3

installerlogipanel.zip

https://platform.netbeans.org/tutorials/60/nbm-login.html