Over a million developers have joined DZone.

Wicket: Lazy Loading TreeTable

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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));


The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}