SWIRL 1.1 - MenuBuilder tutorial

The SWIRL library provides a class MenuBuilder which allows you to easily separate what various menu items and tool bar buttons do on the one hand, and what they should look like in your GUI on the other hand.

The actions that need to be performed by the various buttons are programmed in the usual way, using Swing Actions, ItemListeners, etc. However, the captions, mnemonics, accelerator keys, tool tip texts and icons for the buttons, as well as their positions on various menus and tool bars, are not hardcoded but are instead specified in configuration files. Moreover, captions, mnemonics, acceleration keys and tool tip texts are fully internationalized using the standard resource bundle mechanism.

Basic concepts

Instead of working with menu items or buttons directly, we use a simple abstraction. Buttons and menu items are represented by the same kind of objects but are rendered differently depending on their context: as text-and-icon items in a menu or as icon-only buttons in a tool bar.

One and the same object can be used in many places at the same time and then all rendered versions will be 'synchronised': for example, a toggle button and check box menu item that correspond to the same abstract object will always have the same selection state: either both or none are selected at the same time. Clicking on the toggle button will automatically change the check box state, and conversely.

We distinguish between three different kinds of object:

  • Actions represent push buttons or standard menu items. Their behaviour is controlled by an object of type Action.

  • Toggles represent check box menu items or toggle buttons. They can be selected or deselected independently of other buttons.

  • Selection groups correspond to sets of radio box menu items or toggle buttons that are mutually exclusive. They are used to select a single object among a fixed set of choices and are governed by a selection model of type GenericSelectionModel.

Each of these objects is given a unique identifier.

Note: Support for actions is more sophisticated than for the other two types. For example, changing the caption of an action after the corresponding buttons or menu items are displayed will also dynamically change the appearance of these items. On the other hand, the appearance of toggles and buttons in selection groups is fixed throughout the life time of the application.