Singleton/Non-Singleton TopComponent via Annotations

This is a TopComponent with the annotations created by the New Window wizard:

import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle;

@TopComponent.Description(preferredID = "MySingletonTopComponent",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "org.test.module.MySingletonTopComponent")
@ActionReference(path = "Menu/Window")
@TopComponent.OpenActionRegistration(displayName = "#CTL_MySingletonAction",
preferredID = "MySingletonTopComponent")
    "CTL_MySingletonTopComponent=MySingleton Window",
    "HINT_MySingletonTopComponent=This is a MySingleton window"
public class MySingletonTopComponent extends TopComponent {
     public MySingletonTopComponent() {



However, I don't always want a singleton. So, if you change the annotations slightly, you have a non-singleton. All I did was remove the "preferredID" from the @TopComponent.OpenActionRegistration annotation (i.e., the bit you see in bold above) and now I can open multiple instances of the TopComponent below:

import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle.Messages;

@TopComponent.Description(preferredID = "MyInstanceTopComponent",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "org.test.module.MyInstanceTopComponent")
@ActionReference(path = "Menu/Window")
@TopComponent.OpenActionRegistration(displayName = "#CTL_MyInstanceTopComponentAction")
    "CTL_MyInstanceTopComponent=MyInstance Window",
    "HINT_MyInstanceTopComponent=This is a MyInstance window"
public final class MyInstanceTopComponent extends TopComponent {

    public MyInstanceTopComponent() {




Probably it means that the Action is unable to find the existing instance of the TopComponent, since it doesn't know its ID, and therefore creates a new one.


The annotations are only intended for singleton TCs. For non-singletons, they do not really buy you anything - you can always create a fresh TC of your custom type using its constructor and just call open() on it.

Posted by Jesse Glick on December 22, 2011 at 07:22 AM PST #

So all the @TopComponent.* annotations apply to singletons only? Then how does one dock a non-singleton window into a mode at startup? Via dockInto on a mode?

Posted by Geertjan on December 23, 2011 at 02:27 AM PST #

and what about hiding menus using annotation instead of xml layer file?

Posted by DrAhmedJava on January 04, 2012 at 06:14 PM PST #

Interesting, removing that attribute in annotation, have not helped me. Still the Windows menu entry opened only one instance. But of course, creating new action and creating and opening new instance of Top Componenet by open() method helps.

Posted by Mvek on February 21, 2012 at 02:35 PM PST #

I am afraid this is working for the reference action with path Menu/Window, but by removing preferredID, any ContextAware action launched for instance from a Node, won't create a new instance, unless you instanciate the TopComponent itself. Doing so, you are force to include the dependency between different modules.

Is there any way to make it works from actions without adding the module dependency (invoking via WindowManager.getDefault.findInstace(TOP_COMPONENT_ID))?

Thanks in advance.

Posted by Alberto on September 11, 2012 at 01:52 AM PDT #

On JButton actionlistener, this is my code

TopComponent tcToOpen = WindowManager.getDefault().findTopComponent("MyFormsTopComponent");;

I want to open new Instance of the same TopComponent everytime I click on JButton.

Posted by Awais on November 17, 2014 at 06:13 AM PST #

How to open multiple instances of the TopComponent on button click

Posted by Awais on November 18, 2014 at 03:43 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.


« June 2016