title: Setting primefaces selectOneMenu default value from primefaces tree selected item in jsf with ajax
body: <p>in a first form i ve a primefaces multiple selection tree(populated from LDAP) and in another form i ve a selectOneMenu primefaces component. i would like to make the default selectOneMenu  value(the first displayed on this menu) to be the value of the tree selected node. i ve tryed to use f:ajax.. but it doesnt  work as these elements doesnt belong to the same form (the selectOneMenu name in the render attribute was unknown..)</p>
  
  <p>here is my index.xhtml which contain all these elements : </p>
  
  <pre><code> &lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html 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"&gt;
  &lt;h:head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /&gt;
      &lt;title&gt;Insert title here&lt;/title&gt;
      &lt;script src="JS/general.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" type="text/css" href="CSS/general.css" /&gt;
      &lt;script src="clean/clean-ajax-all.js"&gt;&lt;/script&gt;
      &lt;script&gt;
          var crudmenu = "crudmenu";
          var adduserform = "adduserform";
      &lt;/script&gt;
  &lt;/h:head&gt;
  &lt;body&gt;
  
      &lt;h:form id="form"&gt;
  
          &lt;p:growl id="messages" showDetail="true" escape="false" /&gt;
  
          &lt;p:tree value="#{treeBean.root}" var="node"
              onNodeClick="this.form.submit();" selectionMode="multiple"
              selection="#{treeBean.selectedNodes}" id="treeMultiple"&gt;
  
              &lt;p:treeNode&gt;
                  &lt;h:outputText value="#{node}" /&gt;
              &lt;/p:treeNode&gt;
          &lt;/p:tree&gt;
  
  
          &lt;p:commandButton value="Display Selected" update="messages"
              actionListener="#{treeBean.displaySelectedMultiple}" id="btnDisplay" rendered="false"/&gt;  
  
      &lt;/h:form&gt;
  
  
      &lt;div id="adduserform" name="adduserform"
          style="text-align: center; overflow: hidden; height: 0px; width: 270px; border: #040600 1px;"&gt;
  
  
          &lt;h:form&gt;
              &lt;h:panelGrid border="1" columns="2"&gt;
              ID :  &lt;p:selectOneMenu value="" panelStyle="width:150px"
                      effect="fade" var="p" style="width:160px" filter="true"
                      filterMatchMode="startsWith"&gt;
                      &lt;f:selectItem itemLabel="#{treeBean.selectedNodeValue}"
                          itemValue="" /&gt;
  
                  &lt;/p:selectOneMenu&gt; 
  
  
      objectClass : &lt;p:selectOneMenu value="" panelStyle="width:150px"
                      effect="fade" var="p" style="width:160px" filter="true"
                      filterMatchMode="startsWith"&gt;
                      &lt;f:selectItem itemLabel="Select One" itemValue="" /&gt;
  
                  &lt;/p:selectOneMenu&gt;
  
  
              &lt;/h:panelGrid&gt;
          &lt;/h:form&gt;
      &lt;/div&gt;
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and here is my managed bean  :</p>
  
  <pre><code>package org.primefaces.examples.view;
  
  import java.io.Serializable;
  
  import javax.faces.application.FacesMessage;
  //other imports..
  
  @ManagedBean(name = "treeBean")
  public class TreeBean implements Serializable {
  
  
      private TreeNode root;
  
      private TreeNode[] selectedNodes;
  
      private String selectedNodeValue;
  
  
      public TreeBean() {
  
          populateTreeFromLdap();
  
      }
  
  
      public String getSelectedNodeValue() {
          if (selectedNodes != null &amp;&amp; selectedNodes.length == 1)
              for (TreeNode node : selectedNodes)
              selectedNodeValue = node.getData().toString(); 
          return selectedNodeValue;
      }
  
  
      public void setSelectedNodeValue(String selectedNodeValue) {
          this.selectedNodeValue = selectedNodeValue;
      }
  
  
      public void populateTreeFromLdap(){
       //code to get entries from LDAP..
              }               
  
  
      }
  
      public TreeNode getRoot() {
          return root;
      }
  
      public TreeNode[] getSelectedNodes() {
          return selectedNodes;
      }
  
      public void setSelectedNodes(TreeNode[] selectedNodes) {
          this.selectedNodes = selectedNodes;
      }
  
      public void displaySelectedMultiple(ActionEvent event) {
          if (selectedNodes != null &amp;&amp; selectedNodes.length &gt; 0) {
              StringBuilder builder = new StringBuilder();
  
              for (TreeNode node : selectedNodes) {
                  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);
          }
      }
  }
  </code></pre>
  
  <p>The problem is how to refresh the "#{treeBean.selectedNodeValue}" of the selectOneMenu 's itemLabel after the tree node selection happen..</p>
  
  <p>By the way, i m new in jsf and i m really upset that the traditional ajax use for loading 
  a content in a div from another page doesnt work anymore in jsf..i used that in some previous jsp/servlet applications and it work fine..</p>
  
  <p>thnx for help !!</p>