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

发布于 2024-10-16 02:39:13 字数 547 浏览 13 评论 0原文

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[] {
           }),
       }),
    };

我想动态地创建一个节点,上面的静态示例代码 请帮忙

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[] {
           }),
       }),
    };

I want to create a node as dynacially,above sample code for static
plz help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网名女生简单气质 2024-10-23 02:39:13

请参阅TreeDemo 类。这里动态地从文件根检索值并将该值添加到向量中。请参阅 TreeDemo 类中的 FileTreeModel 内部类。您可以遵循相同的操作并添加类似的动态值。

See the TreeDemo class in UIDemo application. Here dynamically retrieve the values from file root and add that values into vector. See the FileTreeModel inner class from TreeDemo class. You can follow the same thing and add the dynamic values like that.

走过海棠暮 2024-10-23 02:39:13

这可能会有所帮助 -

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;
                }
            };

This might help -

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;
    }
}

And -

  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;
                }
            };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文