Dec 192012
 

The open space on the menu bar of a NetBeans Platform application is often wasted. In the NetBeans IDE, the quick search bar is positioned in that handy location. And your application can have that too of course. However, the question was raised on the forums recently how one would go about adding your own component there. Here is the promised post explaining the steps in detail. The result will look like the screenshot below, with the new User label introduced in the space where the quick search would have been.

Screenshot showing the standalone module running in the IDE

Screenshot showing the standalone module running in the IDE

Step 1: The Module. We need a NetBeans Platform module to work with. So if you don’t have one yet, create one now. It can be either a standalone module or exist as a part of a suite. I created a standalone module. When you run a standalone module, it basically gets installed into an instance of the IDE. That is why the screenshot shows all the bits of the IDE.

Step 2: The Component. Create a new JPanel form (I called mine CustomComponent) and add a JLabel to it. Specify and icon for the label if you wish. The important point to take note of is that you need to specify a size for the panel (resize it in the GUI builder) and then specify that same size as the minimum and maximum size of the panel too. Otherwise the size of the panel will not be what you expected.

Custom component in the GUI builder

Custom component in the GUI builder

Step 3: The Action. Just as the NetBeans IDE 7.2 Plugin Quick Start tutorial describes, create a new Action. Just as the tutorial describes, choose Global Toolbar Button on the GUI Registration page (this will be changed later). Change the action to extend from AbstractAction and implement Presenter.Toolbar, all still as in the tutorial. And in the getToolbarPresenter() method return an instance of CustomComponent.

Then change the @ActionReference annotation’s path to just be “Menu” and position to be 10000.

So at the end of this step we now have the action class below.

import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
import org.openide.util.actions.Presenter;

@ActionID(category = "Build",
id = "za.co.pellissier.componentinmenubar.CustomComponentActionListener")
@ActionRegistration(
displayName = "#CTL_CustomComponentActionListener")
@ActionReference(path = "Menu", position = 10000)
@Messages("CTL_CustomComponentActionListener=CustomComponent")
public final class CustomComponentActionListener
extends AbstractAction implements Presenter.Toolbar {

@Override
public void actionPerformed(ActionEvent e) {
}

@Override
public Component getToolbarPresenter() {
return new CustomComponent();
}
}

Step 4. Alignment. In order to force the component to be right-aligned, we can add a spacer just to the left of it. Here the NetBeans Quick Search Integration Tutorial has a handy example (in the very last section). Create an XML Layer file in your module. The file should be opened on creation. Now add a spacer like in the tutorial, resulting in the layer.xml file looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd">
<filesystem>
    <file name="Spacer.instance">
        <attr name="instanceCreate" methodvalue="javax.swing.Box.createHorizontalGlue"/>
        <attr name="position" intvalue="9999"/>
    </file> 
</filesystem>

Note the position of 9999, which is just to the left of the 10000 of the component.

Step 5. Hiding Quick Search. In practice you could have the quick search and your own component next to one another. However, for the purpose of this tutorial I decided to hide the quick search feature. Modify the layer file once more, so that it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.2//EN" "http://www.netbeans.org/dtds/filesystem-1_2.dtd">
<filesystem>
    <folder name="Menu">
        <file name="org-netbeans-modules-quicksearch-QuickSearchAction.shadow_hidden"/>
    </folder>
    <file name="Spacer.instance">
        <attr name="instanceCreate" methodvalue="javax.swing.Box.createHorizontalGlue"/>
        <attr name="position" intvalue="9999"/>
    </file> 
</filesystem>

That is it. Now you have a custom component being displayed in the empty space on the menu bar.

  One Response to “Adding a Custom Component to the Menu Bar”

  1. Good work Pellissier as you promised…….
    Best regards

 Leave a Reply

(required)

(required)


− two = 7

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>