How to use the tree control on a form.
| |
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on the Form Designer in the Tools Guide |
The following code creates and loads a tree control into a list box control called tcExample. The code uses the add()
method to create a root node with three child nodes, each of which has three grandchild nodes. It also creates an object array that stores each node ID and the parent node ID.
This code is in the formActivate event:
// select the first item cmbxRelationship.setSelectedIndex(0); // Create the array of objects used to store the user data Object objNodes[] = new Object[2]; objNodes[0] = "0"; // Root node has no parent, so set to "0" objNodes[1] = "0"; // Root node is the first, so set id to "0" // Add the root node AgoTreeControlNode nodeRoot = tcExample.add (null, 0, "Root Node", objNodes, null); objNodes, null); // Add three child nodes to the root node for (int i = 1; i < 4; i++) { // Set the user data for the child node objNodes = new Object[2]; objNodes[0] = "0"; // Parent of all child nodes is 0 // Use child counter i to determine id // for the new child node objNodes[1] = String.valueOf(i); // Add the child node AgoTreeControlNode nodeChild = tcExample.add ( nodeRoot, AgcTreeControl.CHILD, "Child " + i, objNodes, null); // Increment the id counter m_id++; // Add three child nodes to each child (GrandChild) for (int j = 1; j < 4; j++) { // Set the user data for the grand child node objNodes = new Object[2]; // Use child counter i to determine // the parent id for the new child node objNodes[0] = String.valueOf(i); // Use grandchild counter j to determine // the id for the new child node objNodes[1] = String.valueOf(i * 3 + j); // Add the grandchild node AgoTreeControlNode nodeGrandChild = tcExample.add (nodeChild, 1, "GrandChild " + i + "," + j, objNodes, null); // Increment the id counter m_id++; } }
add()
method is declared as follows:
AgoTreeControlNode add(AgoTreeControlNode relativeTo, int relationship, String text,Object userData, Image image)
For more information, see Adding a node to the tree control.
The following code uses expandNode()
with getSelectedNode()
to expand the currently selected node in the tree control. In this example, the tree is contained in a list box control named tcExample.
// Expand the currently-selected node. tcExample.expandNode(tcExample.getSelectedNode());
expand()
node method takes a node object as an argument. In this example, the object is returned by the method getSelectedNode()
.
The following code uses the collapseNode()
method with the getSelectedNode()
method to collapse the currently selected node.
// Collapse the currently-selected node tcExample.collapseNode(tcExample.getSelectedNode());
collapseNode()
method takes a node object as an argument. The object is returned by getSelectedNode()
.
The following code first uses getSelectedNode()
to get the currently selected node in tcExample. It then uses the getText()
method to get the text label for the selected node, and setText()
to place the text in the text field fldText.
// Get the selected node. AgoTreeControlNode nodeSelected = tcExample.getSelectedNode(); //If we don't have a valid node, return if (nodeSelected == null) return; // Get the text from selected node and display in fldGetText fldText.setText(nodeSelected.getText());
getSelectedNode()
returns the currently selected node which is stored as nodeSelected.
getText()
method gets the string label for the selected node, and passes this string to setText()
The following code uses the tree control's add()
method to add a new node entered in the text field fldadd to the currently selected node. It determines the relationship of the new node to the selected node by getting the value entered in the combo box cmbxRelationship. The code also stores the ID of the parent node and the sequential number for each node added.
private void handle_btnAdd_actionPerformed(ActionEvent evt) { lblMessage.setText(""); // If no node selected, return AgoTreeControlNode nodeSelected = tcExample.getSelectedNode(); if (nodeSelected == null) { lblMessage.setText(MSG_SELECT_NODE); return; } // If no node name entered, return if (fldAdd.getText().equals("")) { lblMessage.setText(MSG_ENTER_NODE_NAME ); return; } // Get the value from cmbxRelationship to determine // how the new row will be added: // if no relationship specified, return. // 1 = CHILD // 2 = NEXT // 3 = PREVIOUS // 4 = LAST String sValue = (String) cmbxRelationship.getValue(); if (sValue == null) { lblMessage.setText(MSG_ENTER_RELATIONSHIP); return; } int iRelationship = Integer.parseInt(sValue); // Get the user data from the selected node Object[] userData = (Object[]) nodeSelected.getUserData(); // Get the selected node's parent id String sParentNode = (String) userData[0]; // Get the selected node's id String sNodeID = (String) userData[1]; // Re-create the object array userData = new Object[2]; // If we are adding a child node, use the selected // node's id as the parent id of the new node, // otherwise use the selected node's parent id if (iRelationship == 1) userData[0] = sNodeID; else userData[0] = sParentNode; // Increment the id counter m_id++; // Use the new value of the id counter as the id // of the new node userData[1] = "" + m_id; // Add the new node according to the relationship selected tcExample.add (nodeSelected, iRelationship, fldAdd.getText(), userData, null); // Clear the node name from the data-entry field fldAdd.setText(""); }
getSelectedNode()
returns the currently selected node, which is then declared as nodeSelected.
add()
method. The code uses getValue()
to get the string value from the combo box, which it passes to String
. Then Integer.ParseInt()
converts this value to an int.
getUserData()
method.
add()
method is declared as follows:
AgoTreeControlNode add(AgoTreeControlNode relativeTo, int Relationship, String text, Object userData, Image image)
The following code uses the remove()
method with getSelectedNode()
to remove the currently selected node from the tree control. The tree is contained in a list box control named tcExample.
// Remove the currently-selected node tcExample.remove(tcExample.getSelectedNode());
remove()
method takes a node object as an argument. In this example, the object is returned by getSelectedNode()
The following code uses the findNode()
method to find a node in the list box tcExample that matches the text entered in the field fldFind. It then uses setSelectedNode()
to select the node in the list box.
// Find a node matching the text entered into the text field AgoTreeControlNode nodeFound = tcExample.findNode(fldFind.getText(), null, true); // Found? if (nodeFound != null) // Yes, so select it tcExample.setSelectedNode(nodeFound);
findNode()
method is declared as follows:
AgoTreeControlNode findNode(String text, Object userData, Boolean recursive)
The following code gets the int values of a selected node id and its parent node id, and displays them in the text fields fldNode and fldNodeParentID respectively.
private void handle_btnGetUserData_actionPerformed(ActionEvent evt) { lblMessage.setText(""); // Get the currently selected node AgoTreeControlNode node = tcExample.getSelectedNode(); if (node == null) { lblMessage.setText(MSG_SELECT_NODE); return; } // Get the user data from the selected node Object[] userData = (Object[]) node.getUserData(); // Get the selected node's parent id from the user data and // display it in fldParentNodeID fldParentNodeID.setText("Parent Node ID = " + (String) userData[0]); // Get the selected node's id from the user data and display // it in fldNode fldNode.setText("Node ID = " + (String) userData[1]); }
getSelectedNode()
returns the currently selected node, which is stored as node.
getUserData()
method.
setText()
to set the values of the text fields fldParentNodeID and fldNode.