Saturday Mar 29, 2014

Cause a Wizard to Appear Automatically

Sometimes you may want a wizard to automatically appear, as a result of logic, rather than a user action. Normally, the user goes to the New File dialog or the New Project dialog or clicks an Action, such as provided by a menu item or toolbar button, and then a wizard starts up.

However, here's something slightly different. The module depends on the Window System API and hence the @OnShowing annotation is available.

import org.openide.awt.Actions;
public class Startable implements Runnable {
    public void run() {
        if (!new File("/path/to/some/folder").exists()) {
            Actions.forID("File", "org.something.core.CreateFolderWizardAction").actionPerformed(null);

In the "run" method, use "Actions.forID" and pass in the category and the ID of the Action that starts up the wizard. Since when you use the Wizard wizard to create a wizard skeleton, you also get an Action, you should have an Action somewhere in your module structure. Instead of registering the wizard in the Action such that it appears as a toolbar button or menu item, just remove those annotations. The only annotations your Action needs are @ActionID and @ActionRegistration, not @ActionReference.

And with that, the wizard starts up after the application is initialized, automatically.


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« March 2014 »