为什么当 TreeModel 添加新节点时我的 JTree 不更新?
我使用的 DefaultTreeModel
填充了 DefaultMutableTreeNode
的覆盖,它支持选择性地更改树中节点的显示字符串。如下面的代码所示,在我的表单中,我通过在单独的类中创建新节点来填充树,然后通过主数据类型的包装类将它们传递进去。该过程是创建一个新的重写的 DefaultMutableTreeNode
,向其添加子节点(每个 AccessPoint
由一个具有多个子节点的节点表示),然后将其存储以供以后使用用户界面。
我第一次以这种方式添加节点时,效果非常好。使用以下代码添加的任何后续节点实际上都存储在 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...
}
如有必要,我可以在创建节点的位置包含代码,但我认为这不是问题。
I am using a DefaultTreeModel
populated with an override of DefaultMutableTreeNode
which supports optionally changing the display string of a node in a tree. As shown in the code below, in my form I populate the tree with new nodes by creating them in a separate class and then passing them in via a wrapper class for my main data type. The procedure there is to create a new overridden DefaultMutableTreeNode
, add children to it (each AccessPoint
is represented by a node with several child nodes), then store it for later use in the UI.
The first time I add a node this way, it works beautifully. Any subsequent node added with the following code is in fact stored in the DefaultTreeModel
, but the JTree
is not being updated with the new nodes.
Why is it that the JTree
doesn't get populated after the first child is added?
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...
}
I can include the code where I create the node if necessary, but I don't think it is the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
DefaultMutableTreeNode
没有通知DefaultTreeModel
它的子节点已更新。为此,您需要调用表模型中的适当方法(nodesChanged
或类似方法),或者(最好)使用DefaultTreeModel.insertNodesInto
方法。The problem is that
DefaultMutableTreeNode
does not inform theDefaultTreeModel
that its children were updated. To do this you'll either want to call the appropriate method in the table model (nodesChanged
or similar) or (preferably) use theDefaultTreeModel.insertNodesInto
method.您很可能遇到一些线程问题。您的 JTree 已在某个线程中更新,但 JTree 的重要副本(显示在 Swing 事件调度线程 (EDT) 中的副本)对这些更改一无所知。
如果是这种情况,您必须使用以下方法更新 Swing EDT 中的 JTree:
我不知道 JTree...也许您必须在 Swing EDT 中更新 TreeModel。
Quite likely you are having some threading issue. Your JTree is updated is some thread, but the important copy of the JTree, the one that is displayed in the Swing event dispatch thread (EDT), knows nothing of those changes.
If this is the case, you have to update the JTree in the Swing EDT using:
I don't know about JTree... maybe it's the TreeModel you have to update in the Swing EDT.