Tuesday August 04, 2009
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:
@Override
protected void componentActivated() {
Toolbar tb = ToolbarPool.getDefault().findToolbar("name-of-my-toolbar");
if (!tb.isVisible()) {
tb.setVisible(true);
}
}
@Override
public void componentDeactivated() {
Toolbar tb = ToolbarPool.getDefault().findToolbar("name-of-my-toolbar");
if (tb.isVisible()) {
tb.setVisible(false);
}
}
"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.
Aug 04 2009, 05:00:29 AM PDT Permalink
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 07:58 AM PDT #
Geertjan,
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 08, 2009 at 02:35 AM 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()) {
count++;
}
System.out.println(count); //how to migrate this to the NB toolbar
}
});
menu.add(print);
getActions().addAction(ActionFactory.createPopupMenuAction(new PopupMenuProvider() {
public JPopupMenu getPopupMenu(Widget widget, Point localLocation) {
return menu;
}
}));
}
}
Posted by Martin Has on August 12, 2009 at 01:51 PM PDT #
Martin, I answered your question here:
http://netbeans.dzone.com/news/tip-reusing-visual-library
Posted by Geertjan on August 13, 2009 at 04:40 AM PDT #
thx for this amazing tip :)
Posted by Martin Has on August 14, 2009 at 01:45 PM 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
thanks
Posted by Philippe on August 26, 2009 at 06:32 AM 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 08:20 AM PDT #


