title: How do I auto-expand a JTree when setting a new TreeModel?
body: <p>I have a custom <code>JTree</code> and a custom <code>JModel</code>; I would for the JTree to "auto-expand" when I give it a new model. At the moment, it simply collapse all the nodes to the root.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>private class CustomTree extends JTree {
  
      @Override
      public boolean isExpanded(TreePath path) {
          return ((Person) path.getLastPathComponent).hasChildren();
  
  }
  
  private class CustomTreeModel extends TreeModel {
  
      // ... omitting various implementation details
  
      @Override
      public boolean isLeaf(Object object) {
          return !((Person) object).hasChildren();
      }
  
  }
  
  Model model = new Model();
  Person bob = new Person();
  Person alice = new Person();
  bob.addChild(alice);
  model.setRoot(bob);
  JTree tree = new CustomTree(new CustomTreeModel(model));
  </code></pre>
  
  <p>At this point, the tree correctly displays:</p>
  
  <pre><code>- BOB
    - ALICE
  </code></pre>
  
  <p>where Alice is a child of Bob (both in the data and in the visual tree)</p>
  
  <p>However, if I call:</p>
  
  <pre><code>tree.setModel(new CustomTreeModel(model));
  </code></pre>
  
  <p>everything is collapsed:</p>
  
  <pre><code>+ BOB
  </code></pre>
  
  <p><strong>Is there a way to "auto-expand" everything in the tree when setting a new model?</strong></p>