title: Deleting node childs of a Java JTree structure
body: <p>I have a ftp program that retrieve folder data each time expanded. It does this by using a model like this:</p>
  
  <p><pre><code>
      private void FilesTreeTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {<br>
          String path = new String("");</p>
  
  <pre><code> DefaultMutableTreeNode chosen = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();
  
   String[] pathArray = evt.getPath().toString().replaceAll("]", "").split(",");
   for (int i = 1 ; i &lt; pathArray.length ; i++) path += "/"+ pathArray[i].trim();
  </code></pre>
  
  <p>// i were aded chosen.removeAllChildren(); without success
              ftp.GoTo(path);</p>
  
          ArrayList listDir = null;
          listDir = ftp.ListDir();
  
  
          ArrayList listFiles = null;
          listFiles = ftp.ListFiles();
  
  
       DefaultMutableTreeNode  child = null , dir = null , X = null;
  
      //this will add files to tree
     for (int i = 0; i &lt; listFiles.size(); i++) {
       child = new DefaultMutableTreeNode(listFiles.get(i));
       if(listFiles.size() &gt; 0) 
       model.insertNodeInto(child, chosen, 0);
     }
  
     //this will add dirs to list
  
          for (int i = 0; i &lt; listDir.size(); i++) {
          X = new DirBranch("در حال دریافت اطلاعات ...").node();
          dir = new DirBranch( (String) listDir.get(i)).node();
          dir.add(X);
          if(listDir.size() &gt; 0)
          model.insertNodeInto(dir, chosen, 0);
      }
  
   FilesTree.setModel(model);  //this is my Swing JTree
  }
  </code></pre>
  
  <p>the problem is every time i expand the JTree it duplicate list of files and folders. so i tried to use <strong>chosen.removeAllChildren();</strong> @ the top of the code but it didnt remove anything. what should i do?</p>