Loosely Coupled Tabs in Java Editor

One of the NetBeans Platform 7.1 API enhancements is the @MultiViewElement.Registration annotation.

That lets you add a new tab to any existing NetBeans editor. Really powerful since I didn't need to change the sources (or even look at the sources) of the Java editor to add the "Visualizer" tab to it, as shown below:

Right now, the tab doesn't show anything, that will come in the next blog entry. The point here is to show how to set things up so that you have a new tab in the Java editor, without needing to touch any of the NetBeans IDE sources:

And here's the code, take note of the annotation, which registers the JPanel for the "text/x-java" MIME type:

import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.openide.awt.UndoRedo;
import org.openide.loaders.DataObject;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;

@MultiViewElement.Registration(displayName = "#LBL_Visualizer",
iconBase = "org/java/vis/icon.gif",
mimeType = "text/x-java",
persistenceType = TopComponent.PERSISTENCE_NEVER,
preferredID = "JavaVisualizer",
position = 3000)
public class JavaVisualizer extends JPanel implements MultiViewElement {

    private JToolBar toolbar = new JToolBar();
    private DataObject obj;
    private MultiViewElementCallback mvec;

    public JavaVisualizer(Lookup lkp) {
        obj = lkp.lookup(DataObject.class);
        assert obj != null;

    public JComponent getVisualRepresentation() {
        return this;

    public JComponent getToolbarRepresentation() {
        return toolbar;

    public Action[] getActions() {
        return new Action[0];

    public Lookup getLookup() {
        return obj.getLookup();

    public void componentOpened() {

    public void componentClosed() {

    public void componentShowing() {

    public void componentHidden() {

    public void componentActivated() {

    public void componentDeactivated() {

    public UndoRedo getUndoRedo() {
        return UndoRedo.NONE;

    public void setMultiViewCallback(MultiViewElementCallback mvec) {
        this.mvec = mvec;

    public CloseOperationState canCloseElement() {
        return CloseOperationState.STATE_OK;

It's a fair amount of code, but mostly pretty self-explanatory. The loosely coupled tabs are applicable to all NetBeans editors, not just the Java editor, which is why the "History" tab is now available to all editors throughout NetBeans IDE. In the next blog entry, you'll see the integration of the Visual Library into the panel I embedded in the Java editor.


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