Java JTree expand only level one nodes


With a JTree, assuming the root node is level 0 and there may be up to 5 levels below the root, how can I easily expand all the level 1 nodes so that all level 1 & 2 branches and leafs are visible but levels 3 and below aren't?

Best Answer

Thanks for the quick response guys. However I have now found the simple solution I was looking for. For some reason I just couldn't see DefaultMutableTreeNode.getLevel() in the JavaDocs! FYI what I'm doing now is:

DefaultMutableTreeNode currentNode = treeTop.getNextNode();
do {
    if (currentNode.getLevel() == 1) 
        myTree.expandPath(new TreePath(currentNode.getPath()));
    currentNode = currentNode.getNextNode();
} while (currentNode != null);