Help Center

Local Navigation

Create a field to display a tree view

Use a tree view to display objects, such as a folder structure, in a hierarchical manner. A TreeField contains nodes. The highest node is the root node. A node in the tree can have child nodes under it. A node that has a child is a parent node.
  1. Import the required classes and interfaces.
    import net.rim.device.api.ui.component.TreeField;
    import net.rim.device.api.ui.component.TreeFieldCallback;
    import net.rim.device.api.ui.container.MainScreen;
    import java.lang.String;
  2. Implement the TreeFieldCallback interface.
  3. Invoke TreeField.setExpanded() on the TreeField object to specify whether a folder is collapsible. Create a TreeField object and multiple child nodes to the TreeField object. 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);
  4. To repaint a TreeField when a node changes, create a class that implements the TreeFieldCallback interface and implement the TreeFieldCallback.drawTreeItem method. 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);
Next topic: Images

Was this information helpful? Send us your comments.