1 Reply Latest reply on Dec 7, 2012 8:00 AM by _edward_

    Help <rich:tree> child

    sapy87

      Hi, i have created a Simple tree.

      how to insert into a MUSIC folder a child "country, classic,rock, etc...."??? or an arraylist?

       

       

      this is a class:

       

      OPTION TREE

      @ManagedBean

      public class OptionTree implements Serializable {

                

          private static final long serialVersionUID = 1L;

       

          private OptionTreeNode stationRoot = new OptionTreeNode();

       

          private OptionTreeNode stationNodes = new OptionTreeNode();

       

          public OptionTreeNode getStationNodes() {

              return stationNodes;

          }

       

          private String[] kickRadioFeed = { "Music",

       

          "Book",

       

          "Car",

       

          "PC",

       

          "Comics" };

       

       

          public OptionTree() {

              initRichFacesTree();

          }

       

          private void initRichFacesTree() {

              stationRoot.setName("root");

              stationNodes.addChild(0, stationRoot);

       

              for (int i = 0; i < kickRadioFeed.length; i++) {

                  OptionTreeNode child = new OptionTreeNode();

                  child.setName(kickRadioFeed[i]);

                  stationRoot.addChild(i, child);

              } }

             

             

         

         

         

      }

       

       

      Option tree node

      @ManagedBean

      @SessionScoped

      public class OptionTreeNode extends TreeNodeImpl {

          private String name;

       

          public String getName() {

              return name;

          }

       

          public void setName(String name) {

              this.name = name;

          }

       

          public String toString() {

              return this.name;

          }

       

      }

      tree XHTML

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml"

            xmlns:h="http://java.sun.com/jsf/html"

            xmlns:a4j="http://richfaces.org/a4j"

            xmlns:rich="http://richfaces.org/rich"

            xmlns:f="http://java.sun.com/jsf/core"

            xmlns:ui="http://java.sun.com/jsf/facelets">

       

       

      <h:head></h:head>

      <body>

      <h:form>

       

       

      <rich:tree value="#{optionTree.stationNodes}" var="station" toggleType="client" >

       

       

         <rich:treeNode >

       

       

            <h:outputText  value="stations" />

       

       

         </rich:treeNode>

       

       

      </rich:tree>

      </h:form>

                </body>

       

       

      </html>

       

      Please Help me!!!!!!!

        • 1. Re: Help <rich:tree> child
          _edward_

          First of all you have an error in you jsf file

           

          <rich:tree value="#{optionTree.stationNodes}" var="station" toggleType="client" >

             <rich:treeNode >

                <h:outputText  value="stations" />

             </rich:treeNode>

          </rich:tree>

           

          your var name "station" but you use name "stations" in outputText element - it's wrong, You shoud  use "station"

          And it must be an expression like this

           

          <rich:tree value="#{optionTree.stationNodes}" var="station" toggleType="client" >

             <rich:treeNode >

                <h:outputText  value="#{stations}" />

             </rich:treeNode>

          </rich:tree>

           

          if you want add subchildren for folder music you need just add its for appropriate treeNode

           

          for example you can rewrite you code for initRichFacesTree() method like this:

           

          private void initRichFacesTree() {

                  stationRoot.setName("root");

                  stationNodes.addChild(0, stationRoot);

                  for (int i = 0; i < kickRadioFeed.length; i++) {

                      OptionTreeNode child = new OptionTreeNode();

                      if (kickRadioFeed[i].equals("Music")) {

                          String[] musicChildren = {"country", "classic","rock"};

                          for (String musicCh: musicChildren) {

                              OptionTreeNode m_child = new OptionTreeNode();

                              m_child.setName(musicCh);

                              child.addChild(m_child,m_child);

                          }

                      }

                      child.setName(kickRadioFeed[i]);

                      stationRoot.addChild(i, child);

                  }

              }

           

          After bean construct you will have subchildren of folder Music