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

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.


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?

Posted by Carsten Schmalhorst on August 04, 2009 at 12:58 AM PDT #

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.

Posted by Rocco Casaburo on August 07, 2009 at 07:35 PM PDT #

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;

Posted by Martin Has on August 12, 2009 at 06:51 AM PDT #

Martin, I answered your question here:

Posted by Geertjan on August 12, 2009 at 09:40 PM PDT #

thx for this amazing tip :)

Posted by Martin Has on August 14, 2009 at 06:45 AM PDT #

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


Posted by Philippe on August 25, 2009 at 11:32 PM PDT #

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

Posted by Geertjan on August 26, 2009 at 01:20 AM PDT #

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 ?

Posted by Cường on August 11, 2011 at 08:46 PM PDT #

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.


« July 2016