In xhtml, i changed the "action" button for "actionListener" and quit event tag. In the java code, i used constructor instead of "init" method. Try this
the Java code
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
@ViewScoped
@ManagedBean (name="masterpage_bean")
public class TreeBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private TreeNode root;
private TreeNode selectedNode;
public TreeBean() {
root = new DefaultTreeNode("Root", null);
TreeNode node0 = new DefaultTreeNode("Node 0", root);
TreeNode node1 = new DefaultTreeNode("Node 1", root);
TreeNode node2 = new DefaultTreeNode("Node 2", root);
TreeNode node00 = new DefaultTreeNode("Some guy G.J", node0);
TreeNode node01 = new DefaultTreeNode("Another the third", node0);
}
public TreeNode getRoot() {
return root;
}
public TreeNode getSelectedNode() {
return selectedNode;
}
public void setSelectedNode(TreeNode selectedNode) {
this.selectedNode = selectedNode;
}
public void displaySelectedSingle(ActionEvent event) {
if(selectedNode != null) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", selectedNode.getData().toString());
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}
the XHTML
<?xml version="1.0" encoding="UTF-8"?>
<!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:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/facelets"
>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:layout style="min-width:650px;min-height:400px;" id="head">
<p:layoutUnit position="center" size="275" resizable="true">
<p:growl id="messages" showDetail="true" />
<p:panel header="Work">
<h:panelGrid columns="2" cellpadding="5">
<p:tree id="treeSingle" value="#{masterpage_bean.root}" var="node" style="font-size: 12px"
selectionMode="single"
selection="#{masterpage_bean.selectedNode}">
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
<p:commandButton value="Display Selected" actionListener="#{masterpage_bean.displaySelectedSingle}" update="messages" id="btnDisplay"/>
</h:panelGrid>
</p:panel>
</p:layoutUnit>
</p:layout>
</h:form>
</h:body>
</html>