title: Jtree from folder path saved in database
body: <p>i want to create a jtree from folder path saved in database like
  TestFolder/ABCD/CDEF/ghml
  TestFolder/ABCD/THYU/UVWZ iam saving only the path not parent structure so when i try to create tree structure it works fine for single path(TestFolder/ABCD/CDEF/ghml) i want buid a complete tree struture if any one have idea please help</p>
  
  <pre><code>public class MainClass {
  
  public static void main(String[] a) throws ClassNotFoundException, SQLException {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.add(new JTreeEvents());
      f.setSize(500, 500);
      f.setVisible(true);
  }
  }
  
  enter code here
  
   class JTreeEvents extends JPanel {
  
  private String driver = new String("com.mysql.jdbc.Driver");
  private String url = new String("jdbc:mysql:);
  private String user = new String("root");
  private String password = new String("128");
  private Connection dbCon;
  List&lt;String&gt; arrayList;
  Map&lt;String, Map&lt;Integer, String&gt;&gt; map;
  String[] folderArr;
  PreparedStatement ps = null;
  java.sql.Statement s;
  ResultSet r;
  DefaultMutableTreeNode top;
  DefaultTreeModel dt;
  DefaultMutableTreeNode trees;
  DefaultTreeModel model;
  DefaultMutableTreeNode child, subchild;
  //tree  
  
  JTree jtree;
  
  JTextField jtf;
  
  public JTreeEvents() throws ClassNotFoundException, SQLException {
      //------------------------------------------------------------------
      Class.forName(driver);
      dbCon = DriverManager.getConnection(url, user, password);
      s = dbCon.createStatement();
      r = s.executeQuery("SELECT * FROM project p where p.name='MyFolder4'");
      arrayList = new ArrayList&lt;String&gt;();
      map = new HashMap&lt;String, Map&lt;Integer, String&gt;&gt;();
      Hashtable&lt;String, Object&gt; hashtable = new Hashtable&lt;String, Object&gt;();
      DefaultMutableTreeNode root;
      MutableTreeNode parent;
      DefaultMutableTreeNode parentnode;
      TreePath path = null;
      while (r.next()) {
          arrayList.add(r.getString("path"));
      }
  
      for (String str : arrayList) {
          if (str != null) {
              folderArr = str.split("/");
              String projName = str.split("/")[0];
              root = new DefaultMutableTreeNode(projName);
              model = new DefaultTreeModel(root);
              jtree = new JTree(model);
  
              for (int i = 0; i &lt; folderArr.length; i++) {
  
                  if (i == 0) {
                      parent = new DefaultMutableTreeNode(folderArr[0]);
                  } else {
                      path = jtree.getNextMatch(folderArr[i - 1].toString(), 0, Position.Bias.Forward);
                      if (path != null) {
                          System.out.println(path);
                      }
                      parent = (DefaultMutableTreeNode) (MutableTreeNode)                                                            path.getLastPathComponent();                                       
  
                  }  
  enter code here
                  model = (DefaultTreeModel) jtree.getModel();
                  model.insertNodeInto((DefaultMutableTreeNode) new                           DefaultMutableTreeNode(folderArr[i]), parent, parent.getChildCount());
  
                  model.reload();
                  jtree.expandPath(path);
              }
          }
  
      }
  
      int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
      int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
      JScrollPane jsp = new JScrollPane(jtree, v, h);
  
      add(jsp, BorderLayout.CENTER);
  
      jtf = new JTextField("", 20);
      add(jtf, BorderLayout.SOUTH);
  
      jtree.addMouseListener(new MouseAdapter() {
          public void mouseClicked(MouseEvent me) {
              doMouseClicked(me);
          }
      });
  }
  enter code here
  void doMouseClicked(MouseEvent me) {
      TreePath tp = jtree.getPathForLocation(me.getX(), me.getY());
      if (tp != null) {
          jtf.setText(tp.toString());
      } else {
          jtf.setText("");
      }
  }
        private TreePath find(DefaultMutableTreeNode root, String s) {
  @SuppressWarnings("unchecked")
  Enumeration&lt;DefaultMutableTreeNode&gt; e = root.depthFirstEnumeration();
  while (e.hasMoreElements()) {
      DefaultMutableTreeNode node = e.nextElement();
      if (node.toString().equalsIgnoreCase(s)) {
          return new TreePath(node.getPath());
            }
   }
   return null;
      }
   }
  </code></pre>