Adding a Visual Tab to the NetBeans Java Editor

With the upcoming NetBeans Platform 7.1, because of the new ability to add tabs to existing file editors, you'll be able to do this, i.e., you'll be able to add tabs to a file editor "from the outside", via a new plugin that you've created yourself:

What you see above is a new tab in the Java editor, containing a LabelWidget from the Visual Library for each method found in the Java class.

Here's the code and, again, note that this will work in NetBeans Platform 7.1 (i.e., a download of a recent trunk build) only:

@MultiViewElement.Registration(displayName = "#LBL_JAVA_VISUAL",
mimeType = "text/x-java",
persistenceType = TopComponent.PERSISTENCE_NEVER,
preferredID = "JavaVisual",
position = 2000)
public class JavaVisualDesigner extends JPanel implements MultiViewElement {

    private DataObject obj;
    private static LayerWidget layerWidget;
    private JToolBar toolbar = new JToolBar();
    private transient MultiViewElementCallback callback;
    private static Scene scene;

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

    public String getName() {
        return "#LBL_JAVA_VISUAL";

    public JComponent getVisualRepresentation() {
        scene = new Scene();
        layerWidget = new LayerWidget(scene);
        FileObject fo = obj.getPrimaryFile();
        JavaSource js = JavaSource.forFileObject(fo);
        try {
            js.runUserActionTask(new Task<CompilationController>() {
                public void run(CompilationController compilationController) throws Exception {
                    Document document = compilationController.getDocument();
                    if (document != null) {
                        new MemberVisitor(compilationController).scan(compilationController.getCompilationUnit(), null);
            }, true);
        } catch (IOException ex) {
        return scene.createView();

    private static class MemberVisitor extends TreePathScanner<Void, Void> {
        private CompilationInfo info;
        public MemberVisitor(CompilationInfo info) {
   = info;
        public Void visitClass(ClassTree t, Void v) {
            Element el = info.getTrees().getElement(getCurrentPath());
            if (el != null) {
                TypeElement te = (TypeElement) el;
                List enclosedElements = te.getEnclosedElements();
                for (int i = 0; i < enclosedElements.size(); i++) {
                    Element enclosedElement = (Element) enclosedElements.get(i);
                    String name = enclosedElement.getSimpleName().toString();
                    LabelWidget lw = new LabelWidget(scene, name);
                    lw.setPreferredLocation(new Point(i * 20, i * 40));
            return null;

    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 callback) {
        this.callback = callback;

    public CloseOperationState canCloseElement() {
        return CloseOperationState.STATE_OK;



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.


« February 2015