在JTree中分享父母之间的孩子 [英] Sharing children among parents in a JTree

查看:161
本文介绍了在JTree中分享父母之间的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的DefaultMutableTreeNode类,旨在支持许多类型的数据属性之间的健壮连接(对我来说,这些属性可以是字符串,用户定义的标记或时间戳)。

I have a custom DefaultMutableTreeNode class that is designed to support robust connections between many types of data attributes (for me those attributes could be strings, user-defined tags, or timestamps).

当我汇总数据时,我想给用户一个我们目前所见的存储数据的实时预览。出于效率原因,我只想保留一个特定属性的副本,该副本可能与其他属性有很多连接。

As I aggregate data, I'd like to give the user a live preview of the stored data we've seen so far. For efficiency reasons, I'd like to only keep one copy of a particular attribute, that may have many connections to other attributes.

示例:用户定义的标记 LOL发生在五个不同的时间(由TimeStamps表示)。所以我的JTree(显示此信息的类)将有五个父节点(每次发生标记时都有一个节点)。那些父节点应该为LOL标签定义的DefaultMutableTreeNode的ALL SHARE ONE INSTANCE。

Example: The user-defined tag "LOL" occurs at five different times (represented by TimeStamps). So my JTree (the class that is displaying this information) will have five parent nodes (one for each time that tag occured). Those parent nodes should ALL SHARE ONE INSTANCE of the DefaultMutableTreeNode defined for the "LOL" tag.

不幸的是,使用add(MutableTreeNode newChild)从WHATEVER当前父级删除newChild节点是。这真的太糟糕了,因为我希望所有的父节点都有THE SAME子节点。

Unfortunately, using the add(MutableTreeNode newChild) REMOVES newChild from WHATEVER the current parent node is. That's really too bad, since I want ALL of the parent nodes to have THE SAME child node.

这是一张做错的图片(Curtis是作者和他应该出现在所有的节目中

如何在Java中轻松完成此任务?

How can I accomplish this easily in Java?

更新

我一直在查看DefaultMutableTreeNode.add()的代码......我很惊讶它的工作方式(评论)是我的):

I've been looking at the code for DefaultMutableTreeNode.add()... and I'm surprised it works the way it does (comments are mine):

public void add(MutableTreeNode child)
{
    if (! allowsChildren)
        throw new IllegalStateException();

    if (child == null)
        throw new IllegalArgumentException();

    if (isNodeAncestor(child))
        throw new IllegalArgumentException("Cannot add ancestor node.");

    // one of these two lines contains the magic that prevents a single "pointer" from being
    // a child within MANY DefaultMutableTreeNode Vector<MutableTreeNode> children arrays...
    children.add(child); // just adds a pointer to the child to the Vector array?
    child.setParent(this); // this just sets the parent for this particular instance
}


推荐答案

如果你想轻松,你应该放弃共享实际的TreeNodes本身。整个模型建立在每个节点只有一个父节点的假设之上。我将重点放在设计您的自定义TreeNode上,以便多个节点都可以从同一个地方读取数据,从而保持它们同步。

If you want easily, you should probably give up on sharing the actual TreeNodes themselves. The whole model is built on the assumption that each node has only one parent. I'd focus instead on designing your custom TreeNode so that multiple nodes can all read their data from the same place, thereby keeping them synced.

这篇关于在JTree中分享父母之间的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆