Opening a TopComponent per Node

In the Node, displayed in a viewer TopComponent, add an OpenCookie to the Lookup, and define it such that a new editor TopComponent is created only if no currently open editor TopComponent contains the Object in its Lookup:
public class PersonNode extends BeanNode {

    public PersonNode(Person person) throws IntrospectionException {
        this(person, new InstanceContent());

    private PersonNode(final Person person, InstanceContent ic) throws IntrospectionException {
        super(person, Children.LEAF, new AbstractLookup(ic));
        ic.add(new OpenCookie() {
            public void open() {
                TopComponent tc = findTopComponent(person);
                if (tc == null) {
                    tc = new PersonEditorTopComponent(person);

    private TopComponent findTopComponent(Person person) {
        Set openTopComponents = WindowManager.getDefault().getRegistry().getOpened();
        for (TopComponent tc : openTopComponents) {
            if (tc.getLookup().lookup(Person.class) == person) {
                return tc;
        return null;

    public Action[] getActions(boolean context) {
        return new Action[]{SystemAction.get(OpenAction.class)};

Note that we also have an OpenAction from the Actions API in the context menu of the Node. When you right-click the Node, the Open action will be enabled if there's an OpenCookie in the Lookup. As you can see in the constructor above, there is an OpenCookie in the Lookup, with the definition of what should happen when the action is invoked.

When a new TopComponent is created, immediately put the Object into the TopComponent Lookup so that the same TopComponent will be found next time Open is clicked on the Node:

public final class PersonEditorTopComponent extends TopComponent {
    public PersonEditorTopComponent(Person person) {


Note: In the above TopComponent, I have removed all the annotations that are generated into the top of the TopComponent by the New Window wizard.

Complete source code for the above:

PS: I tried using Openable instead of OpenCookie, but the OpenAction doesn't respond to it. 


Is there a way to change the display name of the action shown in the context menu for each node?

In short, if you right-click an "X" node, is there a way to make the context menu display "Open X?"

Posted by Michael Bishop on March 02, 2013 at 08:18 PM PST #

Hi Michael. Good question and here's the answer:

Posted by Geertjan on March 03, 2013 at 12:25 AM PST #

Hi the simpleApp doesn't compile this is the error I get:-
D:\Program Files\NetBeans 7.3\harness\suite.xml:130: Cannot open E:\Repos\7.3\misc\SimpleApp\${}\nbproject\project.xml
BUILD FAILED (total time: 0 seconds)

I think the package is missing (correct me if am wrong)

Posted by guest on July 15, 2013 at 02:21 AM PDT #

I'm new to the application platform and my work has asked that I start creating my swing applications in the module application style for now on. Do you have any examples on how to open a topcomponent based on a custom menu click event. Example: lets say you have two radio buttons as a custom menu kind of like the workflow predicition system oracle provides. If radio button 1 is pressed a topcomponent based on that theme will open, if button 2 a different topcomponent will open.

Posted by guest on January 08, 2014 at 09:24 AM PST #

This should help: Also, drop me an e-mail at and we'll chat further.

Posted by Geertjan on January 08, 2014 at 09:27 AM PST #

I'm unable to compile the provided sources that I checked out from here:

It says it does not find the referenced PersonOpen module.

In fact I was trying to compile the example exactly to understand how to define an action and it seems missing.

I'm using netbeans 8 but I suppose this should not be a problem

Posted by Alex on May 17, 2014 at 06:35 AM PDT #

You were trying to compile the example to understand how to define an action? In that case, get the book and learn about actions -- And why should using NetBeans 8 not be a problem? Clearly the example wasn't made for NetBeans 8, was it?

Posted by Geertjan on May 18, 2014 at 07:24 AM PDT #

I was able to make it work without defining a custom action, I just used the standard open action.

Anyway, the PersonOpen module is missing from the source, isn't?

It's referenced from the makefile but it's not in the checkout.

Posted by Alex on May 18, 2014 at 11:41 AM PDT #

I am migrating my eclipse RCP application to netbeans RCP.
In my case there are 3 different child nodes under one parent node. Now i want to open a wizard window when i right click one of the child node and select some option on the pop up which my right click has generated.
can you please help me about this?

Posted by Shardul Vaidya on November 20, 2015 at 01:41 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed

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 2016