I have a large tree and don't want to load all the nodes unless they are needed. How do I dynamically load nodes and show when a node has children (which may not be loaded yet)?

Jay Detwiler

The JTree has a listener that can help here. The TreeWillExpandListener notifies you with an event before the JTree displays the expansion. In particular, if your implementation of a tree model is made up of Node objects pass the willExpand to the node selected like this:

  public void treeWillExpand (TreeExpansionEvent event) throws ExpandVetoException { 
    final TreePath path = event.getPath (); 
    final Node node = (Node)path.getLastPathComponent(); 
    node.willExpand ();

In the same way, your TreeModel implementation can pass the call to isLeaf to the node of interest which can determine dynamically if it has children or not.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact