Geertjan's Blog

  • October 6, 2008

Custom Toolbar Loaded via Lookup

Geertjan Wielenga
Product Manager
Let's say you want the toolbar to be on the right side of your application. Let's also say that you want the icons to be larger than 16x16 or 24x24. At the same time, you want to use the NetBeans Actions system, so you can't simply create a TopComponent and then put buttons on the TopComponent. How to solve this problem? Use Lookup! Here's how the result looks for me:

First, I removed all the existing toolbars, by simply adding this to the layer:

<file name="Toolbars_hidden"/>

So, now the toolbars were all gone when I ran the application. Then I created a new TopComponent. I also created a new mode specifically for the TopComponent, to position it along the right side of the application.

Next, I exposed my CallableSystemActions via the META-INF/services technique:

This is the complete content of the file in META-INF/services:


Each of the actions looks as follows:

public final class Action1 extends CallableSystemAction {
public void performAction() {
JOptionPane.showMessageDialog(null, "hello from 1");
protected String iconResource() {
return "/org/nb/customtoolbar/pic1.png";
public String getName() {
return "rightSideToolBar";
public HelpCtx getHelpCtx() {
return HelpCtx.DEFAULT_HELP;
protected boolean asynchronous() {
return false;

The only interesting thing is the getName(). I use that to determine whether or not the action should be displayed in the TopComponent. And how are they displayed in the TopComponent? Like this:

private CustomToolbarDisplayerTopComponent() {
//Here we iterate over implementations of 'CallableSystemAction':
for (CallableSystemAction action : Lookup.getDefault().lookupAll(CallableSystemAction.class)) {//We're only interested in an implementation if it has the right name and an icon:
if (action.getName().equals("rightSideToolBar") && action.getIcon() != null) {//Then we add a new button to the JPanel:
addButton(action, jPanel1);
private static void addButton(final CallableSystemAction action, JPanel container) {
JButton button = new JButton();
button.setAlignmentX(Component.CENTER_ALIGNMENT);//The icon is set by calling 'getIcon' on the implementation:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {//The action is performed by calling 'actionPerformed' on the implementation:

And that's all! The TopComponent is in a different module to where the actions are found; there is no direct dependency on them, only on the CallableSystemAction class. And my toolbar is pluggable because anyone can add their own module that implements CallableSystemAction. All that's needed is for their implementation to include the correct name and an icon and then the additional toolbar button will be loaded. Currently, it will be loaded upon restart. But I could add a LookupListener that would detect whenever a new module fitting the requirements is available, at which point the toolbar button would be loaded at runtime.

One potential problem will occur when the user drags the TopComponent to a different position. Potentially the images will then need to be reorganized, so that they're horizontal when they're in a horizontal mode. But apart from that, I think this is a good solution.

Join the discussion

Comments ( 6 )
  • robin Tuesday, October 7, 2008

    I want call netbeans New File standard action in my plugin.But I dont't know what can I do.And this action bundle on my button widget,not netbeans UI. Master,please help me.

  • guest Tuesday, October 7, 2008

    im not really sure what the whole java.net thing is really.. but i would have to say that your page has inspired me...it makes me want to think twice about turning my back on the java net.

  • guest Tuesday, October 7, 2008

    Nice solution but you should combine it with tips for tuning the window system - that way noboy can close the toolbar, move it to some other place. I think this would even be a closer to original toolbar ;-) But even with this I could dock a TopComponent to RightSlidingMode and it would there just to the right of the toolbar. Seems we need more tricks there.

    Which reminds me - how about a RFE for making the MenuBar, ToolBar and StausBar TopComponents and extend the WindowSystem to allow adding TopComponents in an additional fixed way?

  • Sven Reimers Tuesday, October 7, 2008

    Oh and perhaps Presenter.Toolbar would have been nice to use in Lookup as well (or respect those actions implementing it during adding them to the toolbar).

  • Geertjan Wielenga Saturday, October 11, 2008

    Hi Sven. You're right, I will comment on these points in a blog entry soon.

  • Joseph Monday, August 9, 2010

    I really liked the design of ur application. Ur font looks alot like LucidaGrande or LucidaMac. My netbeans and netbeans application on ubuntu is using metal l&f with Dialog font-family. I have tried everything to change the font. How is ur netbeans and netbeans applications using a different font.

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

Integrated Cloud Applications & Platform Services