Using Icons on Hierarchical Trees
Today we built the infrastructure to display hierarchical trees. This involved implementing TreeNode classes for each our objects and defining which object is the child of our current object. We could then point our af:tree at a view object that we built that would know how to display the trees. All good, all working fine.
But our tree nodes represent different types of objects and we want to do the traditional display of an Icon next to our tree node name. To do this, we added an abstract class to our base class:
public abstract String getIcon();
Now each of our tree objects that derives from our base class would have to implement this method and return the name of the icon that best represents them:
@Override
public String getIcon()
{
return "/css/icons/server.png";
}
In our tree component, we create a Node Stamp to represent what we want each tree node to look like. Here's our tree source:
<af:tree id="myTree" value="#{tree_view_object}" var="node" rowSelection="single"
initiallyExpanded="false">
<f:facet name="nodeStamp">
<af:group id="g1">
<af:image id="i1" source="#{node.icon}" inlineStyle="vertical-align:middle;
margin-right:3.0px;"/>
<af:outputText value="#{node.name}" id="ot1"/>
</af:group>
</f:facet>
</af:tree>
Note the inlineStyle value used to make our tree icons line up better with out text. We drew all our icons using 16x16 transparent png files by the way.
Here's what our tree looks like:
![]()
Now to figure out what to do when a user selects something in our tree! That's next....






