How to build and display menus for forms.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on advanced form topics in the Programmer's Guide |
The following code creates a default menu by adding menu items as nodes to a tree menu. It uses add()
to add one node to another, specifying how the added node should be added, that is, whether it should be added as a child node (of the specified node) or as the last node on the same level as the specified node. After each of the top level menus is complete, the code calls expandNode(node)
, which graphically displays the node and its child nodes.
private void handle_btnDefineMenu_actionPerformed(ActionEvent evt) { AgoTreeControlNode node; node = treeMenu.add (null, treeMenu.LAST, "File"); treeMenu.add (node, treeMenu.CHILD, "Save"); treeMenu.add (node, treeMenu.CHILD, "SaveAs"); treeMenu.add (node, treeMenu.CHILD, "Exit"); treeMenu.expandNode(node); node = treeMenu.add (null, treeMenu.LAST, "Edit"); treeMenu.add (node, treeMenu.CHILD, "Cut"); treeMenu.add (node, treeMenu.CHILD, "Copy"); treeMenu.add (node, treeMenu.CHILD, "Paste"); treeMenu.expandNode(node); node = treeMenu.add (null, treeMenu.LAST, "Help"); treeMenu.add (node, treeMenu.CHILD, "About"); }
treeMenu
as its last top-level node. Since this is the first addition to treeMenu, the last node is actually the first node.
add()
method on treeMenu
. With this method, you specify the (existing) node to which you are adding the new node, how you are adding the node (as child node or same-level node), and the menu or menu item name that corresponds to the added node. If you are adding a top-level node such as the "File" menu, set the first parameter to null.
expandNode()
and specify a top-level node (assuming that you have already created its child nodes), this code displays the node (menu) and its immediate child nodes (submenus or menu items). If you do not call expandNode()
, only the node itself is displayed.
add()
method adds a node, it returns the added node. Ensure that you are adding to the correct node by resetting node each time you begin creating nodes at a different level.
expandNode()
. This code displays, by default, the "Help" menu but not the "About" menu item. The user must manually expand "Help" to see "About".
The following code creates a popup menu that is used to add and delete nodes in the menu tree. The addActionListener()adds an ActionListenerobject to the menu item that calls it. This ActionListener receives action events from this menu item.
public void createPopup() { MenuItem menuItem; m_popupMenu = new PopupMenu("Title Goes Here"); m_popupMenu.add(menuItem = new MenuItem ("Insert Before")); menuItem.addActionListener (this); m_popupMenu.add(menuItem = new MenuItem ("Insert After")); menuItem.addActionListener (this); m_popupMenu.add(menuItem = new MenuItem ("Add Child")); menuItem.addActionListener (this); m_popupMenu.addSeparator(); m_popupMenu.add(menuItem = new MenuItem("Edit")); menuItem.addActionListener (this); m_popupMenu.add(menuItem = new MenuItem("Cut")); menuItem.addActionListener (this); m_popupMenu.add(menuItem = new MenuItem ("Paste Before")); menuItem.addActionListener (this); m_popupMenu.add(menuItem = new MenuItem ("Paste After")); menuItem.addActionListener (this); m_popupMenu.add(menuItem = new MenuItem("Delete")); menuItem.addActionListener (this); add (m_popupMenu); }
Popupmenu(label)
constructor and supplying a title.
add()
and instantiating a MenuItem
.
ActionListener
to it to receive action events from its corresponding menu item. This code uses this as the parameter to addActionlistener
listener since this menu builder form implements the ActionListenerinterface
.
addSeparator()
.
The following code shows how to insert, delete, rename nodes in a tree menu. In order to perform the correct operation on the correct node, you must get the selected operation and the selected node. This code is from the menuItemSelected event:
String sAction = actionEvent.getActionCommand(); AgoTreeControlNode node = treeMenu.getSelectedNode(); if (node == null) node = treeMenu.getRootNode(); if (sAction.equals("Insert Before")) { String sMenu = (String) agDialog.showFormDialog ( "Add menu item", "dlgMenuItem"); if (sMenu==null) return; treeMenu.add (node, treeMenu.PREVIOUS, sMenu); return; } if (sAction.equals("Edit")) { if (node == null) return; Hashtable htParm = new Hashtable(); htParm.put ("ITEM", node.getText()); String sMenu = (String) agDialog.showFormDialog ( "Edit menu item", "dlgMenuItem", htParm); if (sMenu==null) return; node.setText (sMenu); return; } if (sAction.equals("Delete")) { if (node == null) return; boolean bResults; if (node.getChildNode() == null){ bResults = agDialog.showMessageYesNo("Removing Node", "Do you want to remove the node?"); if (bResults) treeMenu.remove(node); } else { bResults = agDialog.showMessageYesNo("Node has children", "This will delete all child nodes. Do you want to continue?"); if (bResults) treeMenu.remove(node); } return;
getActionCommand()
gets the name of the selected operation (action) of the popup menu and returns it as a string.
getSelectedNode()
gets the selected node in the tree menu.
showFormDialog()
method displays a dialog box that requests the name of the new node. It returns the user-input value, which is a String. If the user selects the operation "Insert Before", you add the node to the selected one as follows:
treeMenu.add(node, treemenu.PREVIOUS, sMenu);
Similarly, to add a node after the selected one or as a child of the selected one, set the second parameter as appropriate.
treeMenu.add(node, treemenu.NEXT, sMenu); treeMenu.add(node, treemenu.CHILD, sMenu);
showFormDialog()
method. Use the setText()
method.
getChildNode()
returns NULL if there are no child nodes. remove()
deletes the node and the nodes children.
This Menu Builder Example provides a quick way for you to get code for the formActivate()
and menuItemSelected()
methods. To ensure that you get the menu building code for the desired menu tree: