我正在尝试根据数据库结果集生成 JTree。我明白了
Category | Name
-------- | ----
A | 1
B | 2
A | 3
从数据库。如何仅在需要时将类别添加到 JTree?我希望树看起来像:
[Root]
[Category A]
Child 1
Child 3
[Category B]
Child 2
这是我到目前为止所拥有的:
//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;