Expanding Rich tree nodes programmatically

I have found two different methods for expanding nodes in Rich tree  from code, they both take  advantage of component binding and component state.

Tree Setup

	 <rich:treeNode type="simple" >
		<h:outputText value="#{node}"/>        

Only thing to note here is the binding attribute that we setup in our backing bean.

	protected org.richfaces.component.UITree sampleTreeBinding;

for both solutions after we changed the sampleTree we need to update the binding with new value whitch is

// Make sure that we set the new TreeModel on current binding

Solution 1 : Expanding all nodes

TreeState componentState = (TreeState) sampleTreeBinding.getComponentState();
try {
} catch (IOException e) {

This will expand all levels of the nodes.

Solution 2 : Expanding only nodes that meet our criteria

try {
	final TreeState state = (TreeState) sampleTreeBinding.getComponentState();
	sampleTreeBinding.walk(FacesContext.getCurrentInstance(), new DataVisitor(){
		public void process(FacesContext context, Object rowKey, Object argument)
		throws IOException {
			TreeRowKey<Object> row = (TreeRowKey<Object>)rowKey;
			if(row.depth() == 1){
				state.expandNode(sampleTreeBinding, (TreeRowKey<Object>)rowKey);
catch (IOException e) {

Here we are only expanding nodes at depth 1 but it could be anything. Idea is that we are walking the tree
and checking if we are interested in the doing something with that node if so then we can modify it here.

I hope this helps.

9 thoughts on “Expanding Rich tree nodes programmatically”

  1. hi very good Article which solved my requirement. Thanks a lot for that.

    And can u pls give code how to expand the selected node till its leaf node.

    i have tried many times couldn’t get it.

  2. Hey I have one question regarding expanding node.
    My requirement is :
    I have inputText and tree structure. When I entered some thing on input text, tree should automatically expand based on value entered in input text.
    for eg: A is the parent node and under A I have child node called “Awesome”.It I enter Awesome in input text, the tree should expand automatically and it has to show awesome node.
    Is there any way to do this….
    Thanks in Advance..

    1. You could iterate via all the nodes and compare the node value to your textbox via ‘indexOf’ or something similar. This is what ‘Solution 2’ is doing except there the depth of the node is used instead of value.

      So I would have a ‘Textbox’ with onkeyup listener that makes async call to expand the tree.

Leave a Comment

Your email address will not be published. Required fields are marked *