How do I auto-expand a JTree when setting a new TreeModel?

The following worked for me (called after setting the new model):

for (int i = 0; i < tree.getRowCount(); i++) {
    tree.expandRow(i);
}

I had a similar problem.

Your solution (as posted https://stackoverflow.com/a/15211697/837530) seemed to work for me only for the top level tree nodes.

But I needed to expand all the a descendants node. So I solved it with the following recursive method:

private void expandAllNodes(JTree tree, int startingIndex, int rowCount){
    for(int i=startingIndex;i<rowCount;++i){
        tree.expandRow(i);
    }

    if(tree.getRowCount()!=rowCount){
        expandAllNodes(tree, rowCount, tree.getRowCount());
    }
}

which is invoked with

expandAllNodes(tree, 0, tree.getRowCount());

where, tree is a JTree.

Unless someone has a better solution.