DevFaqWindowsNoActionsOnToolbars

I want to disable the popup menu on the toolbars in the main window. How do I do that?

There is no canonical (or pretty) way to do this, but there is a hack you can do - it works in NetBeans 5.0, 5.5 and 6.x (and probably earlier versions but this wasn't tested).

Create the following ModuleInstall class (remember to add a reference to it in the module manifest, e.g.

OpenIDE-Module-Install: org/netbeans/modules/toolbarthing/Installer.class

If you are using 5.0's update 1 of module development support or later, you can just use New File > NetBeans Plug-In Modules > Module Installer):

package org.netbeans.modules.toolbarthing;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import org.openide.modules.ModuleInstall;
import org.openide.windows.WindowManager;
public class Installer extends ModuleInstall implements Runnable {

    public void restored() {
         WindowManager.getDefault().invokeWhenUIReady(this);
    }

    public void run() {
        JToolBar[] tb = findToolbars();
        for (int i = 0; i < tb.length; i++) {
            processToolbar (tb[I]);
        }
    }

    private JToolBar[] findToolbars() {
        List l = new ArrayList();
        JFrame jf = (JFrame) WindowManager.getDefault().getMainWindow();
        findToolbars (jf.getContentPane(), l);
        JToolBar[[ | ]] tb = (JToolBar[[ | ]]) l.toArray(new JToolBar[L.size()]);
        return tb;
    }

    private void findToolbars(Component c, List l) {
        if (c instanceof JToolBar) {
            l.add(c);
        } else if (c instanceof Container) {
            Component[] cc = ((Container) c).getComponents();
            for (int i=0; i < cc.length; i++) {
                findToolbars(cc[I], l);
            }
        }
    }

    private void processToolbar (JToolBar bar) {
        MouseListener[] ml = bar.getMouseListeners();
        for (int i = 0; i < ml.length; i++) {
            if (ml[I].getClass().getName().indexOf("PopupListener") >= 0) {
                bar.removeMouseListener (ml[I]);
            }
        }
    }
}

The above doesn't seems to work in 6.9.1. This is another similar module installer hack to achieve this:

public class Installer extends ModuleInstall
{

  public void restored()
  {
    removeToolbarPopupMenu();
  }

  private void removeToolbarPopupMenu()
  {
    WindowManager.getDefault().invokeWhenUIReady(
      new Runnable()
      {
        @Override
        public void run()
        {
          removeAllPopupListeners(ToolbarPool.getDefault());
        }
      });
  }

  private static void removeAllPopupListeners(Component c)
  {
    if(c instanceof Container)
    {
      for(Component c2 : ((Container)c).getComponents())
      {
        for(MouseListener l : c2.getMouseListeners())
        {
          if(l.getClass().getName().contains("PopupListener"))
          {
            c2.removeMouseListener(l);
            // Uncomment to obtain a similar console output
            // Removing: org.openide.awt.ToolbarPool$PopupListener@1535ac from javax.swing.JPanel[...]
            // Removing: org.openide.awt.ToolbarPool$PopupListener@1535ac from org.openide.awt.Toolbar[File,...]
            //System.out.println("Removing: " + l + " from " + c2);
          }
        }
        findToolbars(c2);
      }
    }
  }
}
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo