在lwuit中动态创建树视图中的节点

时间:2011-02-03 12:02:49

标签: java-me lwuit treemodel

TreeModel model = new TreeModel() {

    Node[] sillyTree =  {

        new Node("X", new Node[] {
           new Node("Child 1", new Node[] {
           }),
           new Node("Child 2", new Node[] {
           }),
           new Node("Child 3", new Node[] {
           }),
       }),
       new Node("Y", new Node[] {
           new Node("A", new Node[] {
           })
       }),
       new Node("Z", new Node[] {
           new Node("A", new Node[] {
           }),
       }),
    };

我想创建一个dynacially节点,上面是static的示例代码 请帮忙

2 个答案:

答案 0 :(得分:0)

请参阅UIDemo应用程序中的TreeDemo类。这里动态检索文件根目录中的值并将该值添加到vector中。请参阅TreeDemo类中的FileTreeModel内部类。您可以按照相同的操作添加动态值。

答案 1 :(得分:0)

这可能会有所帮助 -

class Node {
    Vector children;
    String value;   // Name of the node….

    public Node(String value, Vector children) {
        this.children = children;
        this.value = value;
    }

    public String toString() {
        return value;
    }
}

和 -

  Node[] nodeArray = generateRootNode(); 

  TreeModel model = new TreeModel() {

  Node[] sillyTree = nodeArray;

                public Vector getChildren(Object parent) {
                    Node n = (Node) parent;
                    Object[] nodes = null;
                    Vector v = new Vector();
                    if (parent == null) {
                        nodes = sillyTree;
                    } else {
                        v = n.children;
                    }
                    if (nodes != null) {
                        for (int iter = 0; iter < nodes.length; iter++) {
                            v.addElement(nodes[iter]);
                        }
                    }
                    return v;
                }

                public boolean isLeaf(Object node) {
                    boolean returnValue = false;
                    try {
                        Node n = (Node) node;
                        returnValue = n.children == null || n.children.size() == 0;
                    } catch (ClassCastException ex) {
                        // It means its a child node or a leaf...
                            returnValue = true;
                    }
                    return returnValue;
                }
            };