public static void expandNodes( JTree tree, TreePath parent, boolean expand, int depth ) {
        if( parent == null || depth == 0 )
            return;

        Object node = parent.getLastPathComponent();
        if( node != null ) {
            for( int i = 0; i < tree.getModel().getChildCount( node ); i++ ) {
                Object child = tree.getModel().getChild( node, i );
                TreePath path = parent.pathByAddingChild( child );
                expandNodes( tree, path, expand, depth - 1 );
            }
        }
    
        if( expand ) 
            tree.expandPath( parent );
        else
            tree.collapsePath( parent );
    }