Rajesh has posted 2 posts at DZone. View Full User Profile

Wicket: Lazy Loading TreeTable

  • submit to reddit

I followed the steps mentioned below in order to enable lazy loading of child nodes in TreeTable in Wicket. I did not find a component in Wicket to do this directly. (I may be wrong, as I am new to Wicket)

It seems javax.swing.tree.DefaultTreeModel and javax.swing.tree.DefaultMutableTreeNode are generally used for creating the TreeTable.

The two important things to be done are overidding the isLeaf() method of DefaultMutableTreeNode and then call setAllowsChildren(true) for nodes that will contain children nodes when expanded. It is explained in detail below.

  1. org.apache.wicket.extensions.markup.html.tree.table.TreeTable uses isLeaf() method of javax.swing.tree.DefaultMutableTreeNode to find if the node needs to be rendered as a folder or a leaf. isLeaf() method returns true only if the node has a child node. This behavior needs to be changed for lazy loading. The method needs to return true for a node that can potentially contain a child node and false otherwise. So, in the below code DefaultMutableTreeNode is extended and isLeaf() method is overridden.

       public class MutableTreeNode extends DefaultMutableTreeNode {




    public boolean isLeaf() {

    return !getAllowsChildren();



    Therefore, for any node that can potentially have a child, setAllowsChildren() method should be called

  2. Now create the TreeTable as usual using MutableTreeNode (created earlier) instead of DefaultMutableTreeNode. Just make sure to call the setAllowsChildren(true) for nodes that will have child node loaded when expanded.
    The following is some sample code for creating a TreeTable.

        //Create the initial Tree.
MutableTreeNode rootNode = new MutableTreeNode();
for (VariableGroup group : grps) {
//Create initial nodes.
child = new MutableTreeNode(group);
//The following line will make sure that isLeaf() method will return true for all initial nodes.
//Create the treemodel
DefaultTreeModel model = new DefaultTreeModel(rootNode);

//Now create the TreeTable.
varGrpTree = new TreeTable("treeVariableGroup", new CompoundPropertyModel(model), columns) {

//Override the onJunctionLinkClicked callback method to retrieve the child records and add them as child
protected void onJunctionLinkClicked(AjaxRequestTarget target, javax.swing.tree.TreeNode node){
//Return if the Children are already added.
if (node.getChildCount() != 0) {
MutableTreeNode treeNode = (MutableTreeNode) node;
VariableGroup parentGrp = (VariableGroup) treeNode.getUserObject();
//Retrieve the recorders from Database
List childGrps = variableManager.fetchChildGroup(parentGrp.getId());
MutableTreeNode child = null;
for (VariableGroup group : childGrps) {
child = new MutableTreeNode(group);
//Business rule says if group.getLft().equals(group.getRgt() - 1 is false then the record will have child record.
child.setAllowsChildren(!group.getLft().equals(group.getRgt() - 1));


Your rating: None
Published at DZone with permission of its author, Rajesh Ilango.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Leo Erlandsson replied on Thu, 2010/09/09 - 2:57am

Great article!


Lazy Loading Ajax Trees in Wicket has been discussed at several times on the Wicket User List. Several solutions has also been proposed. Search the Wicket User List, and you'll find more solutions to the problem.






Rajesh Ilango replied on Wed, 2010/10/06 - 12:27pm


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.