i have a database with two columns parent
and child
and it have following data
parent | child
===================
sara | danny
sara | benny
dia | tom
aria | jenny
aria | sunny
and i want to display it in JTree
like this
-parent
*sara
+danny
+benny
*dia
+tom
*aria
+jenny
+sunny
from ResultSet
i am getting each row.. but how to create DefaultMutableTreeNode
only once and add child in single while
loop? how to check if node
is already made?
I found this solution, it could be a wrong approach, But it worked. So i called newRow
function for each row.
TreeMap group = new TreeMap();
void newRow(String Parent,String Child) {
if(group.containsKey(Parent)) {
//if Parent already in group return Arraylist of child
ArrayList<String> al = (ArrayList<String>) group.get(Parent);
//Add new Child to ArrayList
al.add(Child);
}
else {
ArrayList<String> al = new ArrayList<>();
al.add(Child);
//if new Parent add parent and its Child ArrayList
group.put(groupname,al);
}
}
When i iterated through TreeMap group
this was the outPut and i used it to make JTree
sara : [danny , benny]
dia : [tom]
aria : [jenny, sunny]