Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up expand/collapse all nodes of a JTree

I have a JTree with about 100000 nodes or more. Now I want to expand the whole tree. To do so I use the solution I found here.

My problem is that expanding such a large tree takes like 60 seconds or more, which is not very convenient. Does anyone have any suggestions how I could speed up expansion?

like image 412
hagt Avatar asked Dec 15 '09 12:12

hagt


4 Answers

Quick way:

JTree jTree;
for (int i = 0; i < jTree.getRowCount(); i++) {
         jTree.expandRow(i);
}
like image 108
Fry Avatar answered Oct 23 '22 18:10

Fry


I had the same problem with a tree containing 150 000 nodes (with more than 19 000 openable nodes). And I divided by 5 the duration of the expand all just by overriding the method getExpandedDescendants :

JTree tree = new javax.swing.JTree()
{
    @Override
    public Enumeration<TreePath> getExpandedDescendants(TreePath parent)
    {
        if (!isExpanded(parent))
        {
            return null;
        }
        return java.util.Collections.enumeration(getOpenedChild(parent, new javolution.util.FastList<TreePath>()));
    }

    /**
     * Search oppened childs recursively
     */
    private List<TreePath> getOpenedChild(TreePath paramTreeNode, List<TreePath> list)
    {
        final Object parent = paramTreeNode.getLastPathComponent();
        final javax.swing.tree.TreeModel model = getModel();
        int nbChild = model.getChildCount(parent);
        for (int i = 0; i < nbChild; i++)
        {
            Object child = model.getChild(parent, i);
            final TreePath childPath = paramTreeNode.pathByAddingChild(child);
            if (!model.isLeaf(child) && isExpanded(childPath))
            {
                //Add child if oppened
                list.add(childPath);
                getOpenedChild(childPath, list);
            }
        }
        return list;
    }
};

The expand all action take now 5 seconds instead of 25 and I'm still working on improve performance.

like image 35
Gabriel Cauchis Avatar answered Oct 23 '22 17:10

Gabriel Cauchis


I think you need to think of a display strategy, either breadth-first (look at all direct children) or depth-first (look at all the descendants of just one child). 100,000 is far too many nodes to view on the screen and you will need to think about panning and zooming. You should think of filters that could select the subsets of descendants that you want.

One strategy could be to display the top children and when your mouse enters a child, display all its descendants and when you leave collapse them. In that way you could navigate over the tree displaying the current subtree of interest.

like image 35
peter.murray.rust Avatar answered Oct 23 '22 18:10

peter.murray.rust


i tried the solution, you use, too.

After my opinion the code presented there isn't optimal: - it calls tree.expandPath for all the nodes, instead of calling it only for the deepest non-leaf nodes (calling expandPath on leaf nodes has no effect, see the JDK)

Here's a corrected version which should be faster:

// If expand is true, expands all nodes in the tree.
    // Otherwise, collapses all nodes in the tree.
    public void expandAll(JTree tree, boolean expand) {
        TreeNode root = (TreeNode)tree.getModel().getRoot();
        if (root!=null) {   
            // Traverse tree from root
            expandAll(tree, new TreePath(root), expand);
        }
    }

    /**
     * @return Whether an expandPath was called for the last node in the parent path
     */
    private boolean expandAll(JTree tree, TreePath parent, boolean expand) {
        // Traverse children
        TreeNode node = (TreeNode)parent.getLastPathComponent();
        if (node.getChildCount() > 0) {
            boolean childExpandCalled = false;
            for (Enumeration e=node.children(); e.hasMoreElements(); ) {
                TreeNode n = (TreeNode)e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                childExpandCalled = expandAll(tree, path, expand) || childExpandCalled; // the OR order is important here, don't let childExpand first. func calls will be optimized out !
            }

            if (!childExpandCalled) { // only if one of the children hasn't called already expand
                // Expansion or collapse must be done bottom-up, BUT only for non-leaf nodes
                if (expand) {
                    tree.expandPath(parent);
                } else {
                    tree.collapsePath(parent);
                }
            }
            return true;
        } else {
            return false;
        }
    }
like image 41
bxantus Avatar answered Oct 23 '22 16:10

bxantus