Welcome!

Java Authors: Maureen O'Gara, Liz McMillan, Walter H. Pinson, III, Yakov Werde, Tony Bishop

Related Topics: Java

Java: Article

How To Build a Toolbar From a Menu

For better usability, versatility, and user friendliness

JToolBarMenu can be initialized with any other component you would like to add to the toolbar. To do this, a component array can be passed to the JToolBarMenu and added at the beginning, each one divided by a separator.

JToolBarMenu is associated with a configuration file (a simple properties file), which is used to store the sequence of preferred menu items that have to be added to the toolbar for subsequent rebuilding.

Within the configuration file, each menu item is identified by a property; if its value is empty, the item will not be present in the toolbar, otherwise, it will be added.

If the configuration file is empty (see Figure 3) or absent, the toolbar will contain only one button, inviting the user to customize the toolbar. In this case the user should press the "Customize" button to select the preferred functionalities he or she would like to have in the toolbar; a dialog box will appear displaying a JTree that represents the structure of the menubar given as input (see Figures 4 and 5). Each tree element (except root, which identifies the menubar) is editable, meaning that it has a checkbox to specify whether that element should be included in the toolbar (see Figure 6).

As described previously, the grouping of JRadioButtonMenuItems is detected too. A group node is created for them and added to the tree; the JRadioButtonMenuItems appear as group node leaves.

The JRadioButtonMenuItems that are grouped together are initially disabled. They can be selected (or unselected) all together by clicking on their parent group node. The general rule is: by clicking on a leaf element this will be toggled, selected/unselected; by clicking on a node, all its descendant items will be selected/unselected.

For a better view, the tree elements show a double icon: the first one identifies the type of the item (one of the following: menu bar, menu or submenu, radio button, check box, menu item), the second one is the menu item icon provided by the menu designer.

After the first call, customization of the dialog box can be accessed by pressing the special button that's last in the set of buttons in the toolbar (see Figure 7) - (see Figure 8) - (see Figure 9). This action will open a popup menu that shows the option "Customize...". It is advisable to provide the JToolBarMenu application with a predefined configuration file so the user doesn't have to customize the menu on the first run. The most important part of JToolBarMenu is the construction of the Action objects, which keeps the menu items and toolbar buttons linked together.

If a menu item was already built with an action, a new JButton is created by directly passing the menu item action (using the getAction method). Otherwise a new and complete Action object must be constructed, getting the follow-ing information from the menu item:

  • Action name, from the menu item text.
  • Action command key, from the menu item action command.
  • Action Mnemonic key, from the menu item mnemonic key.
  • Action icon, from the menu icon, if any, otherwise an "empty" icon (that is, a 100% transparent icon) will be set. Note that frequently used menu items should always have an icon, if not, an empty button will be created that will be anonymous and hardly distinguishable among others, especially if there are some empty buttons.
  • Action short description, from the menu item tooltip text.
  • Action enable state, from the menu item enable state.
ActionListener objects, if any, are also taken from a menu item. This is important for two reasons: to notify a menu item when a button is pressed so that its functionality can be activated, and to set the selected state when a button is pressed or a menu item is selected. A ChangeListener object is created to reciprocally set the enable state for both the components.

JToolBarMenu is sensitive to the resizing of the frame; when the frame width is reduced, not enough space could be available for displaying all buttons in the toolbar. The solution is to hide the minimum number of buttons that precede the "customize" button so that it will be always visible. An ellipsis ("...") is replaced instead of the hidden buttons. This strategy is also used when, after customizing the toolbar, the user has selected too many buttons to fit the assigned width.

Limitations
Currently there is only one known limitation: the algorithm that's used to represent the menu bar in a JTree may not correctly interpret how to group JRadioButtonMenuItems depending on the menu design.

For example, consider the Alignment menu composed by left, center, right, and justify in this order:

  1. If you put a menu separator between center and right, the algorithm will create two groups instead of one, allowing operations like pressing left and right at the same time;
  2. If you don't insert a separator (or a submenu or something that breaks the continuity in the menu) before left and after justify, the algorithm will extend the grouping to others that will eventually precede or follow JRadioButtonMenuItems and will produce malfunctions. If your menubar contains this kind of menu, your design is probably not in agreement with common GUI guidelines.
Conclusion
This article presented a simple, powerful, and useful tool for an integrated menubar and toolbar GUI design and implementation; this tool matches the needs of both the programmer (because he doesn't have to duplicate code) and the final user (because she can arrange the toolbar, adding or deleting buttons according to her needs). Using it yields some benefits in terms of better usability, versatility, and user friendliness. No relevant overhead has been encountered using JToolBarMenu.

Programmers can insert this component among the customized components in their IDE and use it whenever they want.

Software code was tested within our needs and developed using the Java 2 Platform, Standard Edition, v 1.4.2. The source code is available at zip.html.

References

More Stories By Mauro Micalizzi

Mauro Micalizzi, a researcher, has been involved in software developing for 25 years, and the Java language for seven. He is currently working on GUI, signal processing, and printing framework. Mauro has a degree in computer science.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Java Developer's Journal News Desk 11/10/05 12:45:57 PM EST

Java Developer's Journal Feature: Building a Toolbar From a Menu. Actually I would like to do something more, for instance, providing a toolbar for my application. First, I add three buttons with the same cut, copy, and paste icons (no text for them, according to the Look-and-Feel Design Guidelines), specifying the same tooltip text used for the menu items (note that I do exactly what I did before for the menubar). Then I link the buttons to the code that implements their functionalities (I repeat my actions again). At the end I write the code to disable and enable the buttons, following the same criteria used for the menu items.

JDJ News Desk 11/10/05 12:20:13 PM EST

Java Developer's Journal Feature: Building a Toolbar From a Menu. Actually I would like to do something more, for instance, providing a toolbar for my application. First, I add three buttons with the same cut, copy, and paste icons (no text for them, according to the Look-and-Feel Design Guidelines), specifying the same tooltip text used for the menu items (note that I do exactly what I did before for the menubar). Then I link the buttons to the code that implements their functionalities (I repeat my actions again). At the end I write the code to disable and enable the buttons, following the same criteria used for the menu items.