title: How to build a jtree dynamically
body: <p>The SSCCE of the problem is as follows. I am dynamically populating <code>JTree</code>, but nothing is happening.    </p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.GridLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.Vector;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTree;
  import javax.swing.tree.DefaultMutableTreeNode;
  import javax.swing.tree.DefaultTreeModel;
  import javax.swing.tree.TreePath;
  
  public class DynamicTreeDemo extends JFrame
                          implements ActionListener {
  private int newNodeSuffix = 1;
  private static String ADD_COMMAND = "add";
  private static String REMOVE_COMMAND = "remove";
  private static String CLEAR_COMMAND = "clear";
  
  private JTree jtrMainMenu;
  DefaultMutableTreeNode   rootNode = null;
  DefaultTreeModel treeModel;
  public DynamicTreeDemo() {
      super(new BorderLayout());
  
      //Create the components.
      jtrMainMenu = new JTree();
      populateTree();
  
      JButton addButton = new JButton("Add");
      addButton.setActionCommand(ADD_COMMAND);
      addButton.addActionListener(this);
  
      JButton removeButton = new JButton("Remove");
      removeButton.setActionCommand(REMOVE_COMMAND);
      removeButton.addActionListener(this);
  
      JButton clearButton = new JButton("Clear");
      clearButton.setActionCommand(CLEAR_COMMAND);
      clearButton.addActionListener(this);
  
      //Lay everything out.
      jtrMainMenu.setPreferredSize(new Dimension(300, 150));
      add(jtrMainMenu, BorderLayout.CENTER);
  
      JPanel panel = new JPanel(new GridLayout(0,3));
      panel.add(addButton);
      panel.add(removeButton); 
      panel.add(clearButton);
  add(panel, BorderLayout.SOUTH);
  }
  
  
      public void populateTree() {
    Vector&lt;Vector&lt;String&gt;&gt; data =GetDataFromDB();           
   java.util.List&lt;String&gt; list =GetListFromDB();
          int i=0;;
   int size=list.size();
   treeModel=(DefaultTreeModel)jtrMainMenu.getModel();  
  
   ((DefaultMutableTreeNode)  (treeModel.getRoot())).removeAllChildren();
   treeModel.reload();
   rootNode = new DefaultMutableTreeNode("JewelleryERPApplication");
   treeModel=new DefaultTreeModel(rootNode);
  
  
   DefaultMutableTreeNode p1=null, p2=null,p3=null,p4=null,p5=null;
  
  
       while(i&lt;size){
  
          if(list.get(i).length()==2){
            p1 = addObject(null, data.get(i).elementAt(1),true);  
  
          }else 
          if(list.get(i).length()==4){
             p2 = addObject(p1, data.get(i).elementAt(1),true);   
  
       }else if
         (list.get(i).length()==6){
               p3 = addObject(p2, data.get(i).elementAt(1),true);   
  
       }
       else if
         (list.get(i).length()==8){
               p4 = addObject(p3, data.get(i).elementAt(1),true);    
  
       }  
       else if
         (list.get(i).length()==10){
               p5 = addObject(p4, data.get(i).elementAt(1),true);    
  
       }      
           i++;
       }
  
      jtrMainMenu.setModel(treeModel);
     ((DefaultMutableTreeNode)  (treeModel.getRoot())).getChildCount();
      jtrMainMenu.revalidate();
      jtrMainMenu.repaint();
  }
  
  
  public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                          Object child, 
                                          boolean shouldBeVisible) {
  
  
            DefaultMutableTreeNode childNode = 
              new DefaultMutableTreeNode(child);
  
  
  
      if (parent == null) {
          parent = rootNode;
      }
  
  //It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
      treeModel.insertNodeInto(childNode, parent, 
                               parent.getChildCount());
  
       jtrMainMenu.setModel(treeModel);
  
      //Make sure the user can see the lovely new node.
      if (shouldBeVisible) {
          jtrMainMenu.scrollPathToVisible(new TreePath(childNode.getPath()));
      }
  
      return childNode;
  } 
  
  /**
   * Create the GUI and show it.  For thread safety,
   * this method should be invoked from the
   * event-dispatching thread.
   */
  private static void createAndShowGUI() {
      //Create and set up the window.
      JFrame frame = new JFrame("DynamicTreeDemo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      //Create and set up the content pane.
      DynamicTreeDemo newContentPane = new DynamicTreeDemo();
      newContentPane.setOpaque(true); //content panes must be opaque
      frame.setContentPane(newContentPane);
  
      //Display the window.
      frame.pack();
      frame.setVisible(true);
  }
  
  public static void main(String[] args) {
      //Schedule a job for the event-dispatching thread:
      //creating and showing this application's GUI.
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
              createAndShowGUI();
          }
      });
  }
  }
  </code></pre>