【发布时间】:2011-10-13 11:04:40
【问题描述】:
我正在使用DefaultTreeModel 填充DefaultMutableTreeNode 的覆盖,它支持可选地更改树中节点的显示字符串。如下面的代码所示,在我的表单中,我通过在单独的类中创建新节点然后通过我的主要数据类型的包装类将它们传递给新节点来填充树。该过程是创建一个新的覆盖DefaultMutableTreeNode,向其添加子节点(每个AccessPoint 由一个具有多个子节点的节点表示),然后将其存储以供以后在 UI 中使用。
我第一次以这种方式添加节点时,效果很好。使用以下代码添加的任何后续节点实际上都存储在DefaultTreeModel 中,但JTree 并未使用新节点进行更新。
为什么在添加第一个孩子后JTree 没有被填充?
private void populateAccessPointTreeModel(AccessPointDataWrapper wrapper) {
//the pre-created DefaultMutableTreeNode subclass instance is
// stored in the wrapper
DefaultMutableTreeNode accessPointNode =
wrapper.getAccessPointTreeNode();
//this line updates the accessPointTree with the new node (I've looked at the
// value in debug mode, and it does in fact add the node
((DefaultMutableTreeNode) accessPointTree.getRoot()).add(accessPointNode);
//unrelated logic happens down here...
}
如有必要,我可以在其中包含创建节点的代码,但我认为这不是问题。
【问题讨论】:
-
您是否尝试过重新绘制组件或其父组件?
标签: java swing jtree treemodel