Adding a Menu or Tool Bar

VisualAge provides menu beans from the com.sun.java.swing and java.awt packages, and tool bar beans from the com.sun.java.swing and com.ibm.uvm.abt.edit packages. Although Swing and AWT beans can be mixed, it is inadvisable.

You can add a menu for a window, for a window component, or for another menu. Define a menu with menu choices that call a method or select a setting.

Define a tool bar with buttons and other components that call a method. Tool bars most commonly contain buttons with graphical images for functions such as clipboard and file operations. A JToolBar can contain other components, however, such as a JComboBox with font choices. The JToolBarButton bean is provided as a convenient means of adding a JButton that is tailored for a tool bar. The JToolBarSeparator bean represents a method call to add separation between other components in a JToolBar.

  1. Add one of the following menu or tool bar beans:


    Bean Description
    JMenu or Menu A cascade menu for another menu
    JMenuBar or MenuBar A menu bar for a window
    JPopupMenu or PopupMenu A pop-up menu for window components
    JToolBar A graphical set of tool choices
    JToolBarButton A button for a tool bar
    JToolBarSeparator A visual separator between components in a tool bar

    Adding a menu bar to a window
    Drop a menu bar bean on the window. The menu bar appears in the window. Additionally, a cascade menu is added on the free-form surface and connected to a new menu label on the menu bar.

    Adding a menu to a menu bar
    Drop a menu bean on the menu bar. A menu is added on the free-form surface and connected to a new menu label on the menu bar. If you have already dropped the menu bean on the free-form surface, drag it to the menu bar and drop it to make the connection.

    Adding a cascade menu to another menu
    Drop a menu bean on the base menu you want to cascade from. A menu is added on the free-form surface and connected to a new menu item on the base menu. If you have already dropped the new menu bean on the free-form surface, drag it to the base menu and drop it to make the connection.

    Adding a pop-up menu
    Drop a pop-up menu bean on the free-form surface. To show the menu when the user clicks the pop-up mouse button on a window component, connect the mouse event for the composite bean to a code that displays the menu.

    The following example code determines whether the pop-up mouse button has been clicked, gets the window component, and shows the pop-up menu:

    protected void genericPopupDisplay(java.awt.event.MouseEvent e, java.awt.PopupMenu p) {
       if ((e.isPopupTrigger()))  {
    	  e.getComponent().add(p);
    	  p.show(e.getComponent(), e.getX(), e.getY());
       };
    }
    

    Adding a tool bar
    You can add a JToolBar bean to the content pane of a Swing applet or window. Before you add the tool bar, you should define the layout property of the content pane as a border layout. Add the tool bar to one of the border regions (North, South, East, or West). By default, a tool bar is detachable because the floatable property is set to True. If you want a floatable tool bar to be attachable to any side of the content pane, do not add any other components to the four border regions.

    When you add a JToolBar, a JToolBarButton is added with it. The JToolBarButton is actually a JButton that is tailored for a tool bar. Change properties of the JToolBarButton , such as the icon, to serve your purpose.

    Adding an action to a menu or tool bar
    You can define a subclass of AbstractAction that can be used in one or more Swing menus and tool bars. For example, you can define an action for a clipboard operation and add it to menus and tool bars. You must implement the actionPerformed() method of the subclass. Add the AbstractAction subclass to the free-form surface. Save the bean. Then, edit the get method for the menu or tool bar to add the AbstractAction subclass.

  2. Enable or disable the menu. When a menu is disabled, the user cannot open it. By default, a menu is initially enabled.

    Initially disabling a menu
    Set the enabled property to False in the menu property sheet.

    Disabling a menu when an event occurs
    Connect the event to the menu's enabled property. Then, set the connection parameter value to False.

    Enabling a menu when an event occurs
    Connect the event to the menu's enabled property. Then, set the connection parameter value to True.

  3. Add choices to the menu. Select from the following menu choice beans:


    Bean Description
    JCheckBoxMenuItem or CheckboxMenuItem A menu choice that toggles a setting on and off
    JMenuItem or MenuItem A menu choice that calls a method
    JRadioButtonMenuItem A menu choice that provides one of a set of mutually exclusive setting values
    JSeparator or MenuSeparator A horizontal line that separates groups of related choices

    Adding a choice to a menu
    Drop one of the menu choice beans within a menu at the location you want. Before you release the mouse button, a horizontal cursor line indicates where the choice will be placed.

    Moving a menu choice
    Drag the menu choice and drop it at a new location, either in the same menu or in another menu. Before you release the mouse button, a horizontal cursor line indicates where the choice will be placed.

    Adding a separator to a menu
    Drop a separator bean within a menu at the location you want. Before you release the mouse button, a horizontal cursor line indicates where the separator will be placed.

    Adding a component to a tool bar
    Drop a component bean on a JToolBar at the location you want. Before you release the mouse button, a cursor line indicates where the choice will be placed.

    Moving a component on a tool bar
    Drag the component and drop it at a new location. Before you release the mouse button, a cursor line indicates where the choice will be placed.

    Adding a separator to a tool bar
    Drop a JToolBarSeparator bean on a JToolBar at the location you want. Before you release the mouse button, a cursor line indicates where the separator will be placed.

  4. Define each menu choice to indicate its purpose and, optionally, to provide a keyboard shortcut.

    Defining text for a menu choice
    Enter the text in the value field of the label property in the property sheet.

    Defining a keyboard shortcut for a menu choice
    Set the shortcut property in the property sheet for the menu choice. Select the Shift check box if you want to use the Shift key. Select a unique key choice for the menu item.

  5. Define the function or setting for each menu choice.

    Calling a method from a menu choice
    Connect the actionPerformed(java.awt.event.ActionEvent) event of the menu choice to the method on the target bean. The method is called when the user selects the menu choice.

    Defining the initial state of a check box menu choice
    Set the state property of the menu choice in its property sheet. If the property value is True, the setting is initially on. Otherwise, the setting is initially off.
    Note:This property setting does not affect the appearance of a check box menu item in the Visual Composition Editor.

    Updating a setting for a check box menu choice
    Connect the state property of the menu choice to the property on the target bean. The property is set when the user toggles the menu choice.

  6. Enable or disable menu choices as appropriate. For example, a menu choice might have no meaning when another menu choice is toggled on, or when the user has provide no input in a text field. When a menu choice is disabled, the user cannot select it. By default, a menu choice is initially enabled.

    Initially disabling a menu choice
    Set the enabled property to False in the menu choice property sheet.

    Disabling a menu choice when an event occurs
    Connect the event to the menu choice's enabled property. Then, set the connection parameter value to False.

    Enabling a menu choice when an event occurs
    Connect the event to the menu choice's enabled property. Then, set the connection parameter value to True.

For examples, see the PopupMenuExample class in the com.ibm.ivj.examples.vc.popupmenuexample package, the Amortization class in the com.ibm.ivj.examples.vc.mortgageamortizer package, and the DirectoryExplorer class in the com.ibm.ivj.examples.vc.swing.directoryexplorer package. These examples are shipped in the IBM Java Examples project.


Related procedures
Working with Beans Visually
Using VisualAge Beans in Visual Composition
Adding the IBM Java Examples project

Related references
Menu and Tool Bar Beans