4 Replies Latest reply on Feb 20, 2008 4:53 PM by Henry Mok

    Question about rich tree

    Henry Mok Newbie

      I'm trying to construct a tree and it's not working and I'm not sure what I'm doing wrong. I created a class which impliments treeNode.

      public class tabTreeNode implements TreeNode {...

      within this class, I define it's type as tabTreeNode.

      public String getType() {
      return "tabTreeNode";
      }

      I then use results from a query to construct a tree. This is the fuction to add a node given the parent node.

      private tabTreeNode addNode(String display, tabTreeNode node ) {

      tabTreeNode tabNode = new tabTreeNode();
      tabNode.setTitle(display);
      tabNode.setData(display);
      node.addChild(display, tabNode);

      return tabNode;
      }

      And finally in the jsp, this is what I have to display the tree.

      <rich:tree switchType="client" style="width:300px" value="#{CategoryGroupTreeBean.treeNode}" var="item"
      nodeFace="#{item.type}">

      <rich:treeNode type="tabTreeNode">
      <h:outputText value="#{item.title}" />
      </rich:treeNode>

      </rich:tree>

      When I step through the java code, the tree looks like it's being constructed right. But the page with the tree has nothing in it. Let me know if you need any more info

      I'd appreciate any help on this.

      Henry

        • 1. Re: Question about rich tree
          Henry Mok Newbie

          I figured out the problem. Gotta pay attention to the details.

          • 2. Re: Question about rich tree
            Viggo Navarsete Expert

            Henrym: Would you mind share your solution to the other members here at the forum? I'm sure other people have problems using the tree as well, and all code that can help them is valuable:)

            • 3. Re: Question about rich tree
              Fab Mars Novice

              In the JSP, #{item} refers to some treeNode.data, and not some treeNode itself ; that is #{item} refers to some String passed as "display" in the TabTreeNode constructor here.

              Regarding the <h:outputText value="#{item.title}" /> , #{item} alone would be enough. But even like that the JSP would throw errors since the nodeFace is unreachable (item being a string and not a TabTreeNode instance).

              To make it to work like it's written now, the user has to override getData() in the TabTreeNodeClass and make it return "this".

              Maybe it would be cool to explicitely mention in the RichFaces manual that this is the TreeNode.data you're iterating in a tree, and not the TreeNode temselves.

              • 4. Re: Question about rich tree
                Henry Mok Newbie

                The problem was in this line of code.

                private tabTreeNode addNode(String display, tabTreeNode node ) {

                tabTreeNode tabNode = new tabTreeNode();
                tabNode.setTitle(display);
                tabNode.setData(display);
                ------>node.addChild(display, tabNode);<------

                return tabNode;
                }

                addChild is a function that you must impliment when you create a class that implements TreeNode. In order to get this to work, instead of calling addChild directly, you must use this instead.

                public void addTab(String display, tabTreeNode newTab) {
                addChild(display, newTab);
                newTab.setParent(this);
                }

                You then call addTab and have addTab call addChild. This was in the sample code that I downloaded but I overlooked it. Hope this help someone else.