How to build a menu bar that is displayed in a frame and a popup menu.
See the chapter on advanced form techniques in the Programmer's Guide |
This example illustrates how to build a menubar that is displayed on a window frame and a popup menu that displays when the user uses the system's popup trigger on a label control. It also describes the events that occur when the user makes a menu selection and how to write code that responds to the action.
This example has code for two methods: one that builds a menubar and another that builds a popup menu. These methods can be called from the formActivate event. To respond to a menu selection, you write code for the MenuItemSelected event for standard MenuItems, or the MenuStateChanged event for CheckBoxMenuItems. The example also has sample code for these events.
In the FormActivate event, you can call the two custom methods described in this section to generate the menus. Of course, these two methods aren't the only way to write the code; you could write code for the menus in one or several methods or even in the formActivate event itself.
protected void formActivate() { buildMenuBar(); // custom method that builds a menu bar buildPopupMenu(); // custom method that implements a popup // Any other processing that you want to do in formActivate }
The buildMenuBar()
custom method takes no parameters and returns no values. It builds a MenuBar with a File Menu, a View Menu, and a Help Menu.
The File Menu looks like this.
The View Menu looks like this.
The Help Menu looks like this.
This is the code for the buildMenuBar()
custom method.
public void buildMenuBar() { // Build a menu bar MenuBar mb; Menu fileMenu, viewMenu, helpMenu; MenuItem item; CheckboxMenuItem checkBoxItem; Frame f; // Create the menu bar mb = new MenuBar(); // Create the File Menu fileMenu = new Menu("File"); // Create the File menu with login, logout, and exit commands loginMenuItem = addMenuItem(fileMenu, "Login...", null); logoutMenuItem = addMenuItem(fileMenu, "Logout...", null); // Disable the logout item logoutMenuItem.disable(); // Add a separator and the file menu to the menu bar. fileMenu.addSeparator(); item = addMenuItem(fileMenu, "Exit", null); mb.add(fileMenu); // Create the View Menu viewMenu = new Menu("View"); item = addMenuItem(viewMenu, "Permissions", null); // Add a refresh item, with a shortcut of CTRL-F5 item = addMenuItem(viewMenu, "Refresh", new MenuShortcut(KeyEvent.VK_F5)); // Add check box menu items checkBoxItem = addCheckboxMenuItem(viewMenu, "Bold"); checkBoxItem = addCheckboxMenuItem(viewMenu, "Italic"); // Add the view menu to the menu bar mb.add(viewMenu); // Create the Help Menu helpMenu = new Menu("Help"); item = addMenuItem(helpMenu, "Help Topics...", null); item = addMenuItem(helpMenu, "About SilverStream Menu Example...", null); // Specify a shorter action command for the About MenuItem item.setActionCommand("About"); // Add the help menu to the menu bar mb.add(helpMenu); // Add the menu bar to the form f = getFrame(); f.setMenuBar(mb); }
addMenuItem()
. Constants identifying keys are defined in the JDK's KeyEvent class. If the third argument is null, the MenuItem won't have a shortcut key.
addMenuItem()
method returns a reference to the new MenuItem, but the return value is not used. The item variable is reused for the next MenuItem. Only when we want to make changes to the MenuItem do we need the reference.
item.setActionCommand("About")
method changes the command text associated with the MenuItem. By default, the action command is the text of the selected MenuItem. If the MenuItem text is long (here it is "About SilverStream Menu Example..."), you can change the action command using the setActionCommand()
method. In this case, when the user selects the About SilverStream Menu Example MenuItem, the value returned with the event is simply "About".
The buildPopupMenu()
custom method takes no parameters and returns no values. It builds a PopupMenu that is associated with a label control.
When the popup trigger occurs, it displays the menu on the left, which has a second cascading menu:
public void buildPopupMenu() { MenuItem item; Menu subMenu; // Create a popup menu popup = new PopupMenu(); // Add three items to it item = addMenuItem(popup, "Red", null); item = addMenuItem(popup, "Blue", null); item = addMenuItem(popup, "Green", null); // Add a cascading menu. subMenu = new Menu("Gray"); // Add three items to it item = addMenuItem(subMenu, "Light Gray", null); item = addMenuItem(subMenu, "Gray", null); item = addMenuItem(subMenu, "Dark Gray", null); // Add it to the menu popup.add(subMenu); // And add the popup to the form this.add(popup); }
A popup menu is usually associated with a specific control and is displayed when the user does the system's "popup trigger" action. In the In the mousePressed or mouseReleased event for the control, you can check whether the user has pressed the popup trigger. If so, call the show()
method to display the menu.
private void handle_lblReport_mouseReleased(MouseEvent mouseEvent) { if (mouseEvent.isPopupTrigger()) m_popupMenu.show(treeMenu, mouseEvent.getX(), mouseEvent.getY()); }
The following code for the MenuItemSelected event illustrates how the MenuBar responds to the user's selection of Login, Logout, and Exit. Processing for all standard MenuItems on the menubar and all popups belong in this event.
protected void menuItemSelected(MenuItem menuitem, ActionEvent actionEvent) { String command; command = menuItem.getActionCommand(); // File Menu commands if (command.equals("Login...")) { //Do some type of Login processing... logoutMenuItem.enable(); loginMenuItem.disable(); } else if (command.equals("Logout...")) { //Do some type of Logout processing... logoutMenuItem.disable(); loginMenuItem.enable(); } else if (command.equals("Exit")) { closeDialog(null); } }
The following example changes the Font style (for example, bold or unbold) of a textfield control based on the user's menu selection.
protected void menuItemStateChanged(MenuItem menuItem, ItemEvent itemEvent) { String command; int stateChange; AgoFontIdentifier font; int style; try { command = menuItem.getActionCommand(); stateChange = itemEvent.getStateChange(); if (command.equals("Bold")) { if (stateChange == itemEvent.SELECTED) { font = Text1.getFontIdentifier(); style = font.getStyle(); font.setStyle(style+AgoFontIdentifier.BOLD); Text1.setFontIdentifier(font); } else if (stateChange == itemEvent.DESELECTED) { font = Text1.getFontIdentifier(); style = font.getStyle(); font.setStyle(style-AgoFontIdentifier.BOLD); Text1.setFontIdentifier(font); } } else if (command.equals("Italic")) { if (stateChange == itemEvent.SELECTED) { font = Text1.getFontIdentifier(); style = font.getStyle(); font.setStyle(style+AgoFontIdentifier.ITALIC); Text1.setFontIdentifier(font); } else if (stateChange == itemEvent.DESELECTED) { font = Text1.getFontIdentifier(); style = font.getStyle(); font.setStyle(style-AgoFontIdentifier.ITALIC); Text1.setFontIdentifier(font); } } } catch (Exception e) { agDialog.displayError(e); } }
getStateChange()
for the ItemEvent object.