I'm trying to generate a JTree based on a database result set. I get
Category | Name
-------- | ----
A | 1
B | 2
A | 3
from the database. How can I add the category to the JTree only if needed? I'd like the tree to look like:
[Root]
[Category A]
Child 1
Child 3
[Category B]
Child 2
This is what I have so far:
//Get the blueprints
SqlHelper shelp = new SqlHelper();
ArrayList<BaseInformation> bpList = shelp.getBlueprints();
//Add each to model
for(int x = 0; x < bpList.size(); x++){
BaseInformation info = bpList.get(x);
category = new DefaultMutableTreeNode(info.blueprintCategory);
top.add(category);
category.add(new DefaultMutableTreeNode(info.blueprintName));
}
JTree tree = new JTree(top);
TreeModel model = tree.getModel();
return model;