Tree selectedNode is null

UI Components for JSF
Post Reply
lrochfort
Posts: 3
Joined: 13 Jun 2011, 11:24

14 Jun 2011, 16:35

I'm trying to use the tree component selection mode. I want to select a node and then click a button to perform an action based upon which node was selected.

However, for some reason the selection object in my backing bean is not set. Any advise would be appreciated.

Code: Select all

        <p:tree id="contextTree" value="#{contextTreeBean.contextRoot}" var="node" selectionMode="single" selection="#{contextTreeBean.selectedNode}">
            <p:treeNode>
                <h:outputText value="#{node.name}"/>
            </p:treeNode>
        </p:tree>
        <p:commandButton value="+" actionListener="#{contextTreeBean.createContext()}" update="contextTree"/>

Code: Select all

@ManagedBean
@RequestScoped
public class contextTreeBean {

    @EJB
    private ContextFacadeLocal contextFacade;
    private Context context = new Context();
    private TreeNode contextRoot;
    private TreeNode selectedNode;

    /** Creates a new instance of contextTreeBean */
    public contextTreeBean() {
    }
    
    public void createContext() {
//        context.setName("New Context");
//        context.setParent((Context)selectedNode.getData());
//        contextFacade.create(context);
//        populateContextTree();
    }

    public TreeNode getSelectedNode() {
        return selectedNode;
    }

    public void setSelectedNode(TreeNode selectedNode) {
        this.selectedNode = selectedNode;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public ContextFacadeLocal getContextFacade() {
        return contextFacade;
    }

    public void setContextFacade(ContextFacadeLocal contextFacade) {
        this.contextFacade = contextFacade;
    }

    public TreeNode getContextRoot() {
        return contextRoot;
    }

    public void setContextRoot(TreeNode contextRoot) {
        this.contextRoot = contextRoot;
    }

    @PostConstruct
    private void postConstruct() {
        populateContextTree();
    }

    private void populateContextTree() {
        buildContextTree(new DefaultTreeNode("Root", null), contextFacade.findRootContexts());
    }

    private void buildContextTree(TreeNode parentNode, List<Context> children) {
        for (Context currentContextNode : children) {
            TreeNode tempNode = new DefaultTreeNode(currentContextNode, parentNode);
            buildContextTree(tempNode, currentContextNode.getChildren());
        }
        contextRoot = parentNode;
    }

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 47 guests