Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a database table like the following. I want to make dynamic jtree from the table.

id Name parent
1 color null
2 red 1
3 white 1
4 green 1
5 dark 4
6 light 4

What I have tried:

To retrieve the table from database i already written the code following.

Java
while( rs.next() ) {  

 String parentId = rs.getString("node_Id");  
 String parentName = rs.getString("node_Name");
 String paren = rs.getString("node_Parent");
 int treeLvl = rs.getInt("node_Level");
// the above code is working perfectly fine, the problem start from here

 Map<String, DefaultMutableTreeNode> parentsMap
 = new HashMap<>();
    DefaultTreeModel tbl = (DefaultTreeModel) TreePro.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)tbl.getRoot();

     DefaultMutableTreeNode Root = parentsMap.get(paren);

   Root = new DefaultMutableTreeNode( paren );

   parentsMap.put(parentId, Root );
   root.add(Root); 

 DefaultMutableTreeNode parent = parentsMap.get(parentName);

   parent = new DefaultMutableTreeNode( parentName );
   parentsMap.put(parentId, parent );
   Root.add(parent);

        }

Now it's creates jtree like this

Root
Null
-color
color
-red
color
-white
color
-green
green
-dark
green
-light
Obviously its wrong, how can i make jtree correctly from database table. Now i do hope i established my question in correct way
Posted
Updated 9-May-18 1:42am

1 solution

You are recreating the root node each time in the loop. You should create the root node once at the beginning. Then use the loop to add all the child entries to the root node.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900