Geertjan's Blog

  • August 4, 2009

How to Migrate a Toolbar to the NetBeans Platform (Part 2)

Geertjan Wielenga
Product Manager
Today's question of the day is a follow-up question from the user who inspired yesterday's blog entry:

Thank you for your explanations. following steps of your tutorial, i now have a customized toolbar on my netbeans platform. Another question arised when doing that. How can i display that toolbar only when a specific window is active? the toolbar is useless in other windows.

The answer:

protected void componentActivated() {
Toolbar tb = ToolbarPool.getDefault().findToolbar("name-of-my-toolbar");
if (!tb.isVisible()) {
public void componentDeactivated() {
Toolbar tb = ToolbarPool.getDefault().findToolbar("name-of-my-toolbar");
if (tb.isVisible()) {

"name-of-my-toolbar" is the name of a folder in the layer, within the Toolbars folder.

The result can be seen below, take a look at where the mouse is in the screenshots below to see that the above code works. In other words, the code you see above is in the TopComponent that defines the "Hello Window":

Now my toolbar is only shown when the window to which it relates is active.

Join the discussion

Comments ( 8 )
  • Carsten Schmalhorst Tuesday, August 4, 2009

    Thanks a lot for the tip. I'd just like to know how to set the visibility to false by default. The way you described it here, it works fine, but on startup, the registered Toolbar is set to visible by default. How do I change this behaviour?

  • Rocco Casaburo Saturday, August 8, 2009


    Thank you very much for this post.

    I've been looking for this code tip for a very long time! This is more useful than using xml configuration file, wich could set hidden possibly visible tolbars that you won't hide.

  • Martin Has Wednesday, August 12, 2009

    Geertjan pls help, how to migrate visual library PopupMenu actions to the NetBeans Platform toolbar, eg:


    public class Scene extends GraphScene<MyNode, String> {


    int count = 0;

    public Scene() {


    final JPopupMenu menu = new JPopupMenu();

    JMenuItem print = new JMenuItem("Print number of nodes");

    print.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {

    for (MyNode node : this.getNodes()) {



    System.out.println(count); //how to migrate this to the NB toolbar




    getActions().addAction(ActionFactory.createPopupMenuAction(new PopupMenuProvider() {

    public JPopupMenu getPopupMenu(Widget widget, Point localLocation) {

    return menu;





  • Geertjan Thursday, August 13, 2009

    Martin, I answered your question here:


  • Martin Has Friday, August 14, 2009

    thx for this amazing tip :)

  • Philippe Wednesday, August 26, 2009

    Hi Geertjan,

    you don't happen to have an answer to Carsten Schmalhorst's comment?

    i've been searching with no success

    there is sommething like adding _hidden :

    <folder name="toolbarname_hidden">

    but then it seems no instance is initialized and the toolbarpool returns a null pointer


  • Geertjan Wednesday, August 26, 2009

    Rather than ask questions here, it is far smarter to do so at dev AT openide DOT netbeans DOT org.

  • C&#432;&#7901;ng Friday, August 12, 2011

    Hi Philippe!

    In netbean RCP 6.9.1 in layer.xml U can code:

    <folder name="Toolbars">

    <file name="File_hidden"/>

    <file name="UndoRedo_hidden"/>

    <file name="Run_hidden"/>

    <file name="Clipboard_hidden"/>

    <file name="Memory_hidden"/>


    But i can't hide "Run" button in toolbar in this way ^^. Geertjan , can U help me ?

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.