Creating and Manipulating TopComponent Windows

To create a new UI component, use the Window Component Wizard. It creates a UI component that is a subclass of org.openide.windows.TopComponent, that can be opened in the NetBeans main window. This UI component can be treated as a standard Swing container. There is only one TopComponent of each class in the INTViewer desktop. To create javax.swing.JInternalFrame objects, follow this walkthrough instead.

Creating a new TopComponent window

Right click the project where you wish to create the new class and chose New | Window Component to bring up the wizard. In Step 2 of the wizard, select a Window Position from the pull down list. In Step 3 of the wizard enter a Class Name Prefix - the "TopComponent" suffix will be added to your Class Name Prefix. For instance, if you supply Example, the final class name will be ExampleTopComponent. Please note, on supplying the class name prefix, the wizard supplies a list of the newly Created Files as well as any Modified Files.

WINDOW POSITIONS

  • explorer - the default position of the NetBeans IDE's Projects window (typically, left side)
  • properties - the default position of the NetBeans IDE's Properties window (typically, right side)
  • leftSlidingSide - the TopComponent window will be available as a button on the left sidebar
  • rightSlidingSide - the TopComponent window will be available as a button on the right sidebar
  • bottomSlidingSide - the TopComponent window will be available as a button on the bottom bar
  • editor - the default position of the NetBeans IDE's Source Editor window (typically, center) - INTViewer uses this window position so please refrain from selecting this for new top components.
  • output - the default position of the NetBeans IDE's Output window (typically, bottom)
  • tools - an INTViewer defined mode that will position the window at the lower left (typically where the Navigator window is located)

See also: http://wiki.netbeans.org/DevFaqCustomWindowMode

When you click the Finish button on the wizard, all of the newly created files will be opened in the IDE's Editor window. The make the TopComponent class visible to other classes and packages, change the final class to public final class. Add desired graphics to your new component.

Opening a TopComponent window

To open the new TopComponent window, add a new action. The Action Wizard will create a new action class and if Always Enabled is selected, will specify that the action will be invoked from a menu item in the menu bar, from a toolbar button in a toolbar, or from a keyboard shortcut anywhere in the IDE. In your new action class, add the following to the actionPerformed method.

ExampleTopComponent.findInstance().open();


The default tab position is to the right of all other tabs. You may specify the tab position of your new component. In the following example, the new window tab is placed to the left of all other open window tabs. The requestActive method makes the new window the active window, otherwise the currently active window remains active.

OPEN NEW COMPONENT

public void actionPerformed(ActionEvent e) {
            ExampleTopComponent window = ExampleTopComponent.findInstance();
            window.openAtTabPosition(0);
            window.requestActive();
}

Changing position of a TopComponent window

The mode controls window position and can be reset programmatically by overriding the open method. The following example uses an INTViewer mode named tools to open the TopComponent window at the lower left corner (where Navigator and Inspector components open in the NetBeans IDE).


CHANGING MODE

 /**
  * Opens the top component on the left side of the desktop.
  */
@Override
public void open() {
        Mode mode = WindowManager.getDefault().findMode("tools");
        if (mode != null) {
                mode.dockInto(this);
                super.open();
        }
}