为什么当 TreeModel 添加新节点时我的 JTree 不更新?

发布于 2024-11-26 06:35:09 字数 1041 浏览 2 评论 0原文

我使用的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴随着你 2024-12-03 06:35:09

问题是 DefaultMutableTreeNode 没有通知 DefaultTreeModel 它的子节点已更新。为此,您需要调用表模型中的适当方法(nodesChanged 或类似方法),或者(最好)使用 DefaultTreeModel.insertNodesInto 方法。

DefaultTreeModel model = (DefaultTreeModel)accessPointTree.getModel();
DefaultMutableTreeNode root = model.getRoot();
model.insertNodeInto(accessPointNode, root, root.getChildCount());

The problem is that DefaultMutableTreeNode does not inform the DefaultTreeModel 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 the DefaultTreeModel.insertNodesInto method.

DefaultTreeModel model = (DefaultTreeModel)accessPointTree.getModel();
DefaultMutableTreeNode root = model.getRoot();
model.insertNodeInto(accessPointNode, root, root.getChildCount());
古镇旧梦 2024-12-03 06:35:09

您很可能遇到一些线程问题。您的 JTree 已在某个线程中更新,但 JTree 的重要副本(显示在 Swing 事件调度线程 (EDT) 中的副本)对这些更改一无所知。

如果是这种情况,您必须使用以下方法更新 Swing EDT 中的 JTree:

SwingUtilities.invokeLater(new Runnable() {
  @Override public void run() { ... update jTree here }
});

我不知道 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:

SwingUtilities.invokeLater(new Runnable() {
  @Override public void run() { ... update jTree here }
});

I don't know about JTree... maybe it's the TreeModel you have to update in the Swing EDT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文