views:

344

answers:

3

Hi!

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?

Thanks in advance...

+1  A: 

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.

peter.murray.rust
A: 

Yeah, rethink your UI element. A JTree is not what you are looking for to show 100,000 nodes. Use something where you can see a table and click items to drill-down into a table element. Then have a breadcrumb like history so the user can navigate up the hierarchy..

If you are insistent on having a JTree, there is a way to take over they way it repaints, but I don't know if that will help you with the expand problem.

TheBigS
In principal I totally agree, but right now I'm only trying to find out what is possible with a JTree. And so far I've found out that the tree works ok with such big data structures when it comes to scrolling and panning. But expanding many nodes is extermly time consuming
hagt
A: 

Hi, 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();

        // 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;
        }
    }
bxantus