Opening Multiple TopComponents For One File

In Convert your TopComponent to a MultiViewElement I explained, in the first part of that blog entry, how to open a file represented by a node in an explorer view into a TopComponent (instead of into the Source Editor, where a file opens by default). Basically, as described in that blog entry, you need to extend org.openide.loaders.OpenSupport and implement org.openide.cookies.OpenCookie and org.openide.cookies.CloseCookie. Not very nice code, but it works perfectly.

However, what if you want to open multiple TopComponent at the same time whenever you open a file? There might be different views onto the same file, all provided by different TopComponents. In this case, I've discovered that you need to override the open() method in your OpenSupport class. Then you can open the TopComponents in one of two ways: if you want group behavior you will create a TopComponentGroup and then call open on the group, within the overridden open().

public void open() {;
    TopComponentGroup group = WindowManager.getDefault().findTopComponentGroup("MyGroup");
    if (group != null){;

Alternatively, just call open() on each of the TopComponents separately and then call active() on the TopComponent that should be active when the file is opened. Note that in this case the TopComponent must be a CloneableTopComponent, but that doesn't necessarily mean that the TopComponent will be cloneable, since only TopComponents in 'editor' modes are cloneable, never TopComponents in 'view' modes (i.e., there's no 'Clone Document' menu item on TopComponents in 'view' modes). Below, instead of creating a TopComponentGroup, I simply opened another TopComponent from the open() in the OpenSupport class:

And here's the OpenSupport class:

class DemoOpenSupport extends OpenSupport implements OpenCookie, CloseCookie {

    TwoTopComponent tc;
    String name;

    public DemoOpenSupport(Entry primaryEntry) {
        DemoDataObject dobj = (DemoDataObject) primaryEntry.getDataObject(); = dobj.getName(): = new TwoTopComponent();

    protected CloneableTopComponent createCloneableTopComponent() {
        OneTopComponent tc = new OneTopComponent();
        return tc;

    public void open() {;


How do you create a TopComponentGroup?

How do you register it under a specific name (e.g. "MyGroup", as you suggest)?

Posted by Janez on February 13, 2009 at 06:48 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.


« April 2014