Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Add a TreeField to a device screen

by Retired ‎02-12-2010 03:09 PM - edited ‎09-16-2010 02:50 PM (4,146 Views)



This article applies to the BlackBerry® devices based on Java®.




A TreeField can be added to a BlackBerry device screen by using the TreeField class and TreeFieldCallback interface, which are members of the net.rim.device.api.ui.component package. A TreeField contains parent and child nodes, which display a folder or tree structure relationship between these items. By default, all objects added to the TreeField are visible (all parent and child nodes are shown).


The circular icons that appear beside each node with child nodes are standard indicators of whether an item is expanded or collapsed. If the icon contains a plus sign (+), the node is expandable. If the icon contains a minus sign (-), the node is collapsible.


A TreeFieldCallback interface must be implemented for the TreeField because it is responsible for adding the fields to the tree.


The example below adds a TreeField to a screen:



import java.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

//making class final as this will not be extended
//and allows compiler to optimize the class
final public class SampleTreeField extends UiApplication {
    TreeCallback _myCallback;
    TreeField _myTree;

    public static void main(String[] args)
        // Create a new instance of the application
        // and start the event thread.
        SampleTreeField app = new SampleTreeField();

    public SampleTreeField()
        // Defines several string variables.
        String fieldOne = "Main Folder2";
        String fieldTwo = "Main Folder1";
        String fieldThree = "SubFolder3";
        String fieldFour = "SubFolder2";
        String fieldFive= "SubFolder1";

        // Defines the TreeCallback.
        _myCallback = new TreeCallback();

        // Defines the TreeField.
        _myTree = new TreeField(_myCallback, TreeField.FOCUSABLE);

        // Adds new child nodes to the tree field.
        // The first value is the parent ID
        // (0 if it is the first node).
        // The second value is the string value
        // associated with the new node.
        int node1 = _myTree.addChildNode(0, fieldOne);
        int node2 = _myTree.addChildNode(0, fieldTwo);
        int node3 = _myTree.addChildNode(node1, fieldThree);
        int node4 = _myTree.addChildNode(node2, fieldFour);
        int node5 = _myTree.addChildNode(node2, fieldFive);

        // Defines a new mainscreen.
        MainScreen mainScreen = new MainScreen();
        // Adds the TreeField to mainscreen.
        // Pushes mainScreen onto the display stack and paints it.

    public static class TreeCallback implements TreeFieldCallback
        // The "drawTreeItem" method is invoked
        // when a treefield item requires painting.
        public void drawTreeItem
            (TreeField tree, Graphics g, int node,
            int y, int width, int indent)
            //Retrieves the user-supplied cookie object
            //for the given node and casts to a string.
            Object obj = tree.getCookie(node);

            if (obj instanceof String)
                String text = (String)obj;
                // Draws the text of the treefield item.
                g.drawText(text, indent, y);


Additional Information


For further information on additional methods available for the TreeField, please refer to the appropriate BlackBerry Java Development Environment guide.

Users Online
Currently online: 30 members 1,876 guests
Recent signins:
Please welcome our newest community members: