General


The MenuInterface

Even though the menus in the package jcontrol.ui.vole.menu are of very different graphical design, they don't differ too much in their usage by an application program. This is possible due to the common interface MenuInterface, which is implemented by every menu type and provides a set of "standard methods" for the programming of menus. Table 1 lists those methods and describes their functionality.

MethodDescription
addMenuItem(String item)Adds a new entry to a menu. The item depends on the menu type and may either be an entry for a textual menu or refer to an image resource.
addMenuItem(String item, int pos)Adds a new entry to a menu at the specified position.
removeMenuItem(String item)Removes an entry from a menu.
enableMenuItem(String item, boolean onoff)Specifies whether a menu entry can be chosen (enabled) or not (disabled; shows as grey/dotted entry).
boolean isMenuItemEnabled(String item)Returns whether the specified item is enabled or disabled.
int getSelectedIndex()Returns the index of the currently selected menu item.
boolean select(int index)Selects the menu item specified by index. Returns a boolean which indicates whether the selection has been successful or not.
boolean select(String item)Like select(int index), with the difference that the menu item to be selected is specified by a String.
Table 1: Methods of interface jcontrol.ui.vole.menu.MenuInterface

At first, to be able to use the methods listed above, a menu has to be instantiated. For this purpose, different menu types are available (according to the purpose of the application). These will be presented in the following sections by means of program examples.


Usage of menus

The class Frame of JControl/Vole is designed in a way, that only one menu can be active at a time. To activate a menu and therewith make it visible to the user, the methode Frame.setMenu(Menu m) has to be invoked. The menu will thereon be drawn on the display (possibly occluding other components) and gains the input focus. The user may now choose an entry (up, down, right, left) and select it by pressing the according pushbutton. The UI framework will then remove the menu automatically and trigger an ActionEvent.


Receiving ActionEvents

The are two possibilities to receive menu triggered ActionEvents. Either overwriting the method onActionEvent of the class Frame or adding an ActionListener to the menu by invoking setListener(ActionListener listener). The following source code examples make use of the first variant.

The overwriting method onActionListener of the class Frame should at first check, if a received ActionEvent has been triggered by a menu (and not, for instance, by a Button or other GUI component). The Java operator instanceof suits this purpose:

                if (event.getSource() instanceof Menu) {
              ...
              /* perform actions */
             ...
         }

Afterwards, the selected menu item can be identified by invoking Menu.getSelectedIndex().