Sharing children among parents in a JTree

白昼怎懂夜的黑 提交于 2019-12-22 09:40:11

问题


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.

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.

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.

Here is a picture of DOING IT WRONG (Curtis is the author and he should appear FOR ALL THE SHOWS):

How can I accomplish this easily in Java?

Update

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
}

回答1:


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.




回答2:


I'm not sure it qualifies as easy, but you might look at Creating a Data Model by implementing TreeModel, which "does not require that nodes be represented by DefaultMutableTreeNode objects, or even that nodes implement the TreeNode interface." In addition to the tutorial example, there's a file system example cited here.




回答3:


Unfortunately, I believe the answer is no. In order to do what you're talking about, you would need to have DefaultMutableTreeNode's internal userObject be a pointer to some String, so that all the corresponding DefaultMutableTreeNode's could point to and share the same String object.

However, you can't call DefaultMutableTreeNode.setUserObject() with any such String pointer, because Java does not have such a concept on the level that C or C++ does. Check out this outstanding blog article on the confusing misconceptions about pass-by-value and pass-by-reference in Java.

Update: Responding to your comment here in the answer space, so I can include a code example. Yes, it's true that Java works with pointers internally... and sometimes you have to clone an object reference to avoid unwanted changes to the original. However, to make a long story short (read the blog article above), this isn't one of those occasions.

public static void main(String[] args) {

    // This HashMap is a simplification of your hypothetical collection of values,
    // shared by all DefaultMutableTreeNode's
    HashMap<String, String> masterObjectCollection = new HashMap<String, String>();
    masterObjectCollection.put("testString", "The original string");

    // Here's a simplification of some other method elsewhere making changes to 
    // an object in the master collection
    modifyString(masterObjectCollection.get("testString"));

    // You're still going to see the original String printed.  When you called 
    // that method, a reference to you object was passed by value... the ultimate
    // result being that the original object in you master collection does 
    // not get changed based on what happens in that other method.
    System.out.println(masterObjectCollection.get("testString"));
}

private static void modifyString(String theString) {
    theString += "... with its value modified";
}

You might want to check out the JIDE Swing extensions, of which some components are commercial while others are open source and free as in beer. You might find some kind of component that comes closer to accomplishing exactly what you want.



来源:https://stackoverflow.com/questions/3849530/sharing-children-among-parents-in-a-jtree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!