Sunday Jul 22, 2012


If you want something to happen when a project conforming to your project type opens or closes, put an instance of org.netbeans.spi.project.ui.ProjectOpenedHook into your project type's lookup. The methods "projectOpened" and "projectClosed" are called when a project of your type opens or closes in your application's GUI.

However, let's make it more interesting! Let's say you want to integrate a tool into NetBeans IDE, for example, that will process ANY project as soon as it is opened. For that purpose, you're able to add objects into the lookup of project types over which you have no control.

Here's an example. Whenever any NetBeans project opens, a balloon message is created, i.e., shown in the bottom right of the IDE, with the possibility to search Google for information relating to the project.

The code, but note that below the ProjectOpenedHook is added to the lookup of four NetBeans projects, though it could be added to many others too, for each the annotation needs to include the related project ID:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.netbeans.api.project.Project;
import org.netbeans.spi.project.LookupProvider;
import org.netbeans.spi.project.ui.ProjectOpenedHook;
import org.openide.awt.HtmlBrowser.URLDisplayer;
import org.openide.awt.NotificationDisplayer;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;

@LookupProvider.Registration(projectType = {
public class DefaultProjectOpenedHook implements LookupProvider {

    public Lookup createAdditionalLookup(final Lookup lookup) {
        Project p = lookup.lookup(Project.class);
        final String name = p.getProjectDirectory().getName();
        return Lookups.fixed(new ProjectOpenedHook() {
            protected void projectOpened() {
                        //display name:
                        "Opened " + name,
                        ImageUtilities.loadImageIcon("org/netbeans.png", false),
                        "Search Google for " + name + "!",
                        //action listener:
                        new ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                                try {
                                    String searchText = URLEncoder.encode(name, "UTF-8");
                                            new URL(""
                                            + searchText + "&btnG=Google+Search"));
                                } catch (Exception eee) {
                                    return;//nothing much to do


            protected void projectClosed() {


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.


« July 2012 »