Over a million developers have joined DZone.

Adding Quick Toolbars to Explorer Views

DZone 's Guide to

Adding Quick Toolbars to Explorer Views

· Java Zone ·
Free Resource
Quick Toolbar in Explorer ViewThe Explorer View provided by NetBeans Platform is extremely useful and powerful. Often, I find that my applications will have several explorer views each with different modes (explorer, navigator, etc). The application will usually have menu items, toolbars on the top, and right-click options on the nodes in the explorer view. I have noticed that sometimes, it is not hard to get lost or even to intuitively know which global toolbar actions go with which explorer view (other than watching the enabled/disabled state toggle as I click the different nodes).  I came up with a quick method to remove some of the clutter from the global toolbar and more closely associate view-specific actions with the related explorer view.

In my applications, usually each explorer view belongs to its own module and therefore the actions offered by that module are only applicable to the view(s) that belong to that module. So moving these actions down to the relevant explorer view makes sense. Also, this relieves the user of the burden to surf through the potential myriad of global toolbar buttons to find the action for which he/she is looking. Since the user’s eyes are already focused in the explorer view, placing the actions in a toolbar within the view makes finding the actions easier and also reduces the distance that the user has to move the mouse to find the action. The image above illustrates the concept (the red outline has been added to draw the reader’s attention).

The implementation of this is fairly simple:

  1. In the TopComponent containing your Explorer view, place your view (e.g., BeanTreeView) in a JPanel and set the TopComponent’s layout to BorderLayout. Set the DIRECTION property of this JPanel to CENTER.

  2. Add JToolBar to your TopComponent. Set the DIRECTION property to SOUTH.

  3. Add actions to your module as needed either by editing the layer.xml file manually or using the New Action wizard.

For this example, we will assume that when you created your actions, you created a shadow entry in the global toolbar under “Toolbars/Widgets”.

<folder name="Toolbars">
<folder name="Widgets">
<file name="org-drx-modules-widgetview-actions-CreateWidgetAction.shadow">
<attr name="originalFile" stringvalue="Actions/Widgets/org-drx-modules-widgetview-actions-CreateWidgetAction.instance"/>

I added a method named initMyToolbar() to the TopComponent. This method finds all of the actions for Widget within the Toolbars folder and adds them to the JToolbar control.

public void initMyToolbar() {
List<? extends Action> alist = Utilities.actionsForPath("Toolbars/Widgets/");
for (Action action : alist) {

Invoke the initMyToolbar() method at the end of the constructor for the TopComponent containing the view and viola, functional toolbar buttons right next to the view itself. I typically set the floatable property of the JToolbar object to false so that the toolbar will stay in place, right next to the view to which it belongs.

From http://www.rigsby.org


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}