Help Center

Local Navigation

Create a field to display a parent and child relationship between items

A TreeField contains parent and child nodes.
  1. Import the following classes:
    • net.rim.device.api.ui.component.TreeField
    • java.lang.String
    • net.rim.device.api.ui.container.MainScreen
  2. Import the net.rim.device.api.ui.component.TreeFieldCallback interface.
  3. Implement the TreeFieldCallback interface.
  4. Invoke TreeField.setExpanded() on the TreeField object to specify whether a folder is collapsible. In the following code sample, we create a TreeField object and multiple child nodes to the TreeField object. We then invoke TreeField.setExpanded() using node4 as a parameter to collapse the folder.
    String fieldOne =  new String("Main folder");
    ...
    TreeCallback myCallback = new TreeCallback();
    TreeField myTree = new TreeField(myCallback, Field.FOCUSABLE);
    int node1 = myTree.addChildNode(0, fieldOne);
    int node2 = myTree.addChildNode(0, fieldTwo);
    int node3 = myTree.addChildNode(node2, fieldThree);
    int node4 = myTree.addChildNode(node3, fieldFour);
    ...
    int node10 = myTree.addChildNode(node1, fieldTen);
    myTree.setExpanded(node4, false);
    ...
    mainScreen.add(myTree);
    
  5. To repaint a TreeField when a node changes, create a class that implements the TreeFieldCallback interface and implement the TreeFieldCallback.drawTreeItem method. In the following code sample, the TreeFieldCallback.drawTreeItem method uses the cookie for a tree node to draw a String in the location of a node. The TreeFieldCallback.drawTreeItem method invokes Graphics.drawText() to draw the String.
    private class TreeCallback implements TreeFieldCallback {
    public void drawTreeItem(TreeField _tree, Graphics g, int node, int y, int width, int indent) {
    String text = (String)_tree.getCookie(node); 
    g.drawText(text, indent, y);  
    }
    }
    

Index


Was this information helpful? Send us your comments.