java - JSF/ICEfaces 动态层次结构树示例

标签 java jakarta-ee tree icefaces

我有部门列表,每个部门可能有父级没有,部门域对象如下:

- departmentId
- parentDepartmentId (null if current department has no parent i,e should be under root directly, and have value if current department have parent).
.
.
.

查看用于创建基本树的icefaces教程代码:

// create root node with its children expanded
    DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
    IceUserObject rootObject = new IceUserObject(rootTreeNode);
    rootObject.setText("Root Node");
    rootObject.setExpanded(true);
    rootTreeNode.setUserObject(rootObject);

    // model is accessed by by the ice:tree component via a getter method, this object is what's needed in the view to display the tree
    model = new DefaultTreeModel(rootTreeNode);

    // add some child nodes
    for (int i = 0; i <3; i++) {
        DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
        IceUserObject branchObject = new IceUserObject(branchNode);
        branchObject.setText("node-" + i);
        branchNode.setUserObject(branchObject);
        rootTreeNode.add(branchNode);
    }

这都是关于构建基本节点并添加子节点。

我的情况很复杂,根下的子A可能有子节点B、C、DD,例如子节点等等。

所以我正在考虑如何完成类似事情的最佳实践,我需要示例代码或提示(如果有人可以提供帮助)。

最佳答案

您需要一种递归方法来从模型构建树。

public void buildRecursiveTreeNode(DefaultMutableTreeNode parentTreeNode,
            String treeId, String treeName, GenericTreeVo modelVo) 
    {
            // if the database model contains more children. 
            // add the current nodes first and pass in this nodes tree id and name to construct the children for this parent nodes.


    }

更新了答案以包括递归示例。

http://www.danzig.us/java_class/recursion.html

刚刚添加了一个递归链接,我想说的是,当您从数据库迭代数据时,您会看到是否有任何子记录,如果您有子记录,您将通过传递 DefaultMutableTreeNode 来调用相同的方法,它将成为父记录。

关于java - JSF/ICEfaces 动态层次结构树示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8170793/

相关文章:

java - Jackson:基于实现接口(interface)的对象身份序列化/反序列化

java - 键绑定(bind)代码不起作用

java - 一个项目 - 多个程序

web-services - 通过 MQ 进行安全调用后无法调用安全 Web 服务

java - 使用 JAX-WS for SOAP 的 WSDL 之外的信息?

java - 在两个不同的二叉搜索树之间查找具有键的公共(public)节点

algorithm - 证明两棵树变得相等的最大旋转次数

java - jbyteArray 和 jbyte 指针的区别

java - 控制台应用程序中出现延迟加载错误

Haskell 非二叉树