我有一个包含重复条目的列表。我正在使用哈希表来存储此列表中每个唯一条目。每个键都将指向DefaultMutableTreeNode类型的对象,该对象表示JTree节点。我的目标是使此节点指向与其父节点相同的节点的所有列表条目。
我已成功添加了这些父节点,但当添加子节点时(通过insertNodeInto),只会出现父节点。代码片段如下;非常感谢您的建议/时间。
我已成功添加了这些父节点,但当添加子节点时(通过insertNodeInto),只会出现父节点。代码片段如下;非常感谢您的建议/时间。
// an unsorted list of items
List<MyObject>list = hash.get(key);
// clause to check for size
if (list.size() > 0) {
// iterate through each item in the list and fetch obj
for (int i=0; i < list.size(); i++) {
MyObject be = list.get(i);
// if object is not in hash, add to hash and point obj to new node
if (!hashParents.containsKey(be)) {
DefaultMutableTreeNode parent =
new DefaultMutableTreeNode(be.getSequence());
// add the key and jtree node to hash
hashParents.put(be, parent);
// insert node to tree, relead model
((DefaultMutableTreeNode)(root)).insert(
new DefaultMutableTreeNode("parent node"),
root.getChildCount());
((DefaultTreeModel)(tree.getModel())).reload();
}
// now that a parent-node exists, create a child
DefaultMutableTreeNode child = new DefaultMutableTreeNode("child");
// insert the new child to the parent (from the hash)
((DefaultTreeModel)(tree.getModel())).insertNodeInto(child, hashParents.get(be),
hashParents.get(be).getChildCount());
// render the tree visible
((DefaultTreeModel)(tree.getModel())).reload();
}
}