title: Checkbox selection in primefaces tree table not working
body: <p>I'm creating a multi tab page by using primefaces .</p>
  
  <p>The Page may contain an add Button if the person click the add button the tab dynamically added.</p>
  
  <p>In that dynamic tab, I enclose a Tree Table by using  with a checkbox selection.</p>
  
  <p>In this page every value is stored in the bean list which is the value of a tabview.</p>
  
  <p><strong>The Problem is:</strong></p>
  
  <p>I got the "selectionNode" Value in the first tab but not able to get the value in other tabs. In log the selection Node value is null in the other tabs. I tried the technique of changing the scope of session and view. But Failed.</p>
  
  <p><em>I enclosed all the code below.</em></p>
  
  <p>Kindly help me over it.</p>
  
  <p><strong>Index.xhtml</strong></p>
  
  <pre><code>&lt;ui:composition xmlns="http://www.w3.org/1999/xhtml"
                  xmlns:h="http://java.sun.com/jsf/html"
                  xmlns:f="http://java.sun.com/jsf/core"
                  xmlns:p="http://primefaces.org/ui"
                  xmlns:ui="http://java.sun.com/jsf/facelets" 
                  xmlns:c="http://java.sun.com/jsp/jstl/core"&gt;
  
      &lt;h:head&gt;
          &lt;title&gt;
              Dynamic View in List
          &lt;/title&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;f:event listener="#{DynamicTab.initializePageAttributes}" type="preRenderComponent"/&gt;
          &lt;h:form id="dynamicFormId"&gt;
              &lt;p:panel id="mainPanelId" widgetVar="mainPanelId"&gt;
                  &lt;p:commandButton id="addButtonId" icon="ui-icon-circle-plus" action="#{DynamicTab.addButtonAction}"
                                   update="mainPanelId" rendered="#{DynamicTab.addButtonRendered}"/&gt;
                  &lt;p:growl id="msgs" showDetail="true" escape="false" /&gt;
  
                  &lt;p:tabView id="tabViewId" activeIndex="#{DynamicTab.activeIndex}" value="#{DynamicTab.sampleList}" var="useCase"
                             dynamic="true" &gt;
                      &lt;p:ajax event="tabChange" listener="#{DynamicTab.onTabChange}" update="@([id$=mainPanelId])"/&gt;
                      &lt;p:ajax event="tabClose" listener="#{DynamicTab.onTabClose}" update="@([id$=mainPanelId])"/&gt;
  
                      &lt;p:tab  id="tabId" closable="true" title="#{useCase.sstId}"&gt;
  
                          &lt;p:panel id="sampleId"&gt;
  
                              &lt;h:outputText value="SST Id: "/&gt;  &lt;h:outputText value="#{useCase.sstLabel}"/&gt;
  
                              &lt;p:commandButton id="uploadButtonId" icon="ui-icon-circle-arrow-n" actionListener="#{DynamicTab.uploadButtonAction}"
                                               update="@([id$=mainPanelId])"/&gt;
  
                              &lt;p:treeTable value="#{useCase.rootNode}"   
                                           var="document" selectionMode="checkbox" selection="#{useCase.selectedNodes}" &gt;
  
                                  &lt;f:facet name="header"&gt;
                                      Tab View
                                  &lt;/f:facet&gt;
  
                                  &lt;p:column headerText="Name"&gt;
                                      &lt;h:outputText value="#{document.name}" /&gt;
                                  &lt;/p:column&gt;
  
                                  &lt;p:column headerText="Size"&gt;
                                      &lt;h:outputText value="#{document.size}" /&gt;
                                  &lt;/p:column&gt;
  
                                  &lt;p:column headerText="Type"&gt;
                                      &lt;h:outputText value="#{document.type}" /&gt;
                                  &lt;/p:column&gt;
  
                              &lt;/p:treeTable&gt;
  
  
                          &lt;/p:panel&gt;
                      &lt;/p:tab&gt;
                  &lt;/p:tabView&gt;
              &lt;/p:panel&gt;
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/ui:composition&gt;
  </code></pre>
  
  <p><em>By click the Add / Upload Button Action I use the Java Code below:</em></p>
  
  <p><strong>DynamicTab.java</strong></p>
  
  <pre><code>public class DynamicTab
  {
      private HtmlForm initForm;
      private int count = 0;
      private Logger logger = Logger.getLogger(getClass());
      private List&lt;DynamicBean&gt; sampleList = new ArrayList&lt;DynamicBean&gt;();
      private Short activeIndex;
      private boolean addButtonRendered;
      private TreeNode root;
  
      public void initializePageAttributes()
      {
          logger.info("Enter inside initializepage");
          count = 0;
          if (sampleList != null &amp;&amp; !sampleList.isEmpty())
          {
              sampleList.clear();
          }
          addButtonRendered = true;
      }
  
      private void addValues()
      {
          formTree();
          DynamicBean dynamic = new DynamicBean();
          dynamic.setSstId("SST" + count);
          dynamic.setSstLabel("SST0" + count);
          dynamic.setTabIndex(String.valueOf(count));
          dynamic.setActiveIndex(activeIndex);
          dynamic.setRootNode(root);
          dynamic.setSelectedNodes(null);
          sampleList.add(dynamic);
      }
  
      private void formTree()
      {
          root = new CheckboxTreeNode(new Document("Files", "-", "Folder"), null);
  
          TreeNode documents = new CheckboxTreeNode(new Document("Documents", "-", "Folder"), root);
          TreeNode pictures = new CheckboxTreeNode(new Document("Pictures", "-", "Folder"), root);
          TreeNode movies = new CheckboxTreeNode(new Document("Movies", "-", "Folder"), root);
  
          TreeNode work = new CheckboxTreeNode(new Document("Work", "-", "Folder"), documents);
          TreeNode primefaces = new CheckboxTreeNode(new Document("PrimeFaces", "-", "Folder"), documents);
  
          //Documents
          TreeNode expenses = new CheckboxTreeNode("document", new Document("Expenses.doc", "30 KB", "Word Document"), work);
          TreeNode resume = new CheckboxTreeNode("document", new Document("Resume.doc", "10 KB", "Word Document"), work);
          TreeNode refdoc = new CheckboxTreeNode("document", new Document("RefDoc.pages", "40 KB", "Pages Document"), primefaces);
  
          //Pictures
          TreeNode barca = new CheckboxTreeNode("picture", new Document("barcelona.jpg", "30 KB", "JPEG Image"), pictures);
          TreeNode primelogo = new CheckboxTreeNode("picture", new Document("logo.jpg", "45 KB", "JPEG Image"), pictures);
          TreeNode optimus = new CheckboxTreeNode("picture", new Document("optimusprime.png", "96 KB", "PNG Image"), pictures);
  
          //Movies
          TreeNode pacino = new CheckboxTreeNode(new Document("Al Pacino", "-", "Folder"), movies);
          TreeNode deniro = new CheckboxTreeNode(new Document("Robert De Niro", "-", "Folder"), movies);
  
          TreeNode scarface = new CheckboxTreeNode("mp3", new Document("Scarface", "15 GB", "Movie File"), pacino);
          TreeNode carlitosWay = new CheckboxTreeNode("mp3", new Document("Carlitos' Way", "24 GB", "Movie File"), pacino);
  
          TreeNode goodfellas = new CheckboxTreeNode("mp3", new Document("Goodfellas", "23 GB", "Movie File"), deniro);
          TreeNode untouchables = new CheckboxTreeNode("mp3", new Document("Untouchables", "17 GB", "Movie File"), deniro);
  
      }
  
      public String addButtonAction()
      {
          addButtonRendered = sampleList.size() &lt; 4;
          count = count + 1;
          addValues();
          return null;
      }
  
      public void onTabChange(TabChangeEvent event)
      {
          FacesMessage msg = new FacesMessage("Tab Changed", "Active Tab: " + event.getTab().getTitle());
          FacesContext.getCurrentInstance().addMessage(null, msg);
      }
  
      public void onTabClose(TabCloseEvent event)
      {
          FacesMessage msg = new FacesMessage("Tab Closed", "Closed tab: " + event.getTab().getTitle());
          FacesContext.getCurrentInstance().addMessage(null, msg);
          TabView tabView = (TabView) event.getComponent();
          addButtonRendered = true;
          List&lt;DynamicBean&gt; tempBeanList = new ArrayList&lt;DynamicBean&gt;();
          tempBeanList.addAll(sampleList);
          for (DynamicBean dynamicBean : tempBeanList)
          {
              if (dynamicBean.getSstId().equalsIgnoreCase(event.getTab().getTitle()))
              {
                  sampleList.remove(dynamicBean);
              }
          }
  
      }
  
      public String uploadButtonAction()
      {
          DynamicBean dynamic = sampleList.get(activeIndex);
          if (dynamic != null)
          {
              if (dynamic.getSelectedNodes() != null)
              {
                  for (TreeNode subTree : dynamic.getSelectedNodes())
                  {
                      Document doc = (Document) subTree.getData();
                      logger.info(" Name: " + doc.getName());
                      logger.info(" Type:  " + doc.getType());
                      logger.info(" Size:  " + doc.getSize());
                  }
              }
              else
              {
                  logger.info("The Selected node is null");
              }
          }
          else
          {
              logger.info("Dynamic is null");
          }
          return null;
      }
  
      public void displaySelectedMultiple(TreeNode[] nodes)
      {
          if (nodes != null &amp;&amp; nodes.length &gt; 0)
          {
              StringBuilder builder = new StringBuilder();
  
              for (TreeNode node : nodes)
              {
                  builder.append(node.getData().toString());
                  builder.append("&lt;br /&gt;");
              }
  
              FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", builder.toString());
              FacesContext.getCurrentInstance().addMessage(null, message);
          }
      }
  
      /**
       * @return the initForm
       */
      public HtmlForm getInitForm()
      {
          return initForm;
      }
  
      /**
       * @param initForm the initForm to set
       */
      public void setInitForm(HtmlForm initForm)
      {
          this.initForm = initForm;
      }
  
      /**
       * @return the sampleList
       */
      public List&lt;DynamicBean&gt; getSampleList()
      {
          return sampleList;
      }
  
      /**
       * @param sampleList the sampleList to set
       */
      public void setSampleList(List&lt;DynamicBean&gt; sampleList)
      {
          this.sampleList = sampleList;
      }
  
      /**
       * @return the activeIndex
       */
      public Short getActiveIndex()
      {
          return activeIndex;
      }
  
      /**
       * @param activeIndex the activeIndex to set
       */
      public void setActiveIndex(Short activeIndex)
      {
          this.activeIndex = activeIndex;
      }
  
      /**
       * @return the addButtonRendered
       */
      public boolean isAddButtonRendered()
      {
          return addButtonRendered;
      }
  
      /**
       * @param addButtonRendered the addButtonRendered to set
       */
      public void setAddButtonRendered(boolean addButtonRendered)
      {
          this.addButtonRendered = addButtonRendered;
      }
  }
  </code></pre>
  
  <p><em>The Bean Class Dynamic Bean Contains the following variables and their getter/setters:</em></p>
  
  <p><strong>DynamicBean.Java</strong></p>
  
  <pre><code>public class DynamicBean
  {
      private String sstId;
      private String sstLabel;
      private String tabIndex;
      private String pagePath;
      private Short activeIndex;
      private TreeNode rootNode;
      private TreeNode[] selectedNodes;
  }
  </code></pre>
  
  <p><em>The Tree Table Bean Class :</em></p>
  
  <p><strong>Document.Java</strong></p>
  
  <pre><code>    public class Document implements Serializable, Comparable&lt;Document&gt;
      {
  
      private String name;
      private String size;
      private String type;
  
      public Document(String name, String size, String type)
      {
          this.name = name;
          this.size = size;
          this.type = type;
      }
  
      public String getName()
      {
          return name;
      }
  
      public void setName(String name)
      {
          this.name = name;
      }
  
      public String getSize()
      {
          return size;
      }
  
      public void setSize(String size)
      {
          this.size = size;
      }
  
      public String getType()
      {
          return type;
      }
  
      public void setType(String type)
      {
          this.type = type;
      }
  
      //Eclipse Generated hashCode and equals
      @Override
      public int hashCode()
      {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((name == null) ? 0 : name.hashCode());
          result = prime * result + ((size == null) ? 0 : size.hashCode());
          result = prime * result + ((type == null) ? 0 : type.hashCode());
          return result;
      }
  
      @Override
      public boolean equals(Object obj)
      {
          if (this == obj)
          {
              return true;
          }
          if (obj == null)
          {
              return false;
          }
          if (getClass() != obj.getClass())
          {
              return false;
          }
          Document other = (Document) obj;
          if (name == null)
          {
              if (other.name != null)
              {
                  return false;
              }
          }
          else if (!name.equals(other.name))
          {
              return false;
          }
          if (size == null)
          {
              if (other.size != null)
              {
                  return false;
              }
          }
          else if (!size.equals(other.size))
          {
              return false;
          }
          if (type == null)
          {
              if (other.type != null)
              {
                  return false;
              }
          }
          else if (!type.equals(other.type))
          {
              return false;
          }
          return true;
      }
  
      @Override
      public String toString()
      {
          return name;
      }
  
      public int compareTo(Document document)
      {
          return this.getName().compareTo(document.getName());
      }
  }
  </code></pre>