两个属性中的引用都在JTree中发生变化

时间:2013-07-30 10:30:14

标签: java swing jtree

在此之前,我很抱歉我的英语不好,并告诉我是否有任何不可理解的事情。

我有2个Jtree。每棵树显然都有相同的信息。唯一改变的是具有每个节点的属性的名称。

例如 JTree1有一个ID和一个ParentID。这些属性具有名称和值。名称:ID_Tree1。值:TESTID1 //名称:ParentID_Tree1。值:TESTPID1 在JTree2中,它具有与JTree1中相同的值,但名称不同。

有一段时间我将节点从JTree1传输到JTree2来创建它。传输/创建是正确的,但是当我读取节点时,它具有不同的属性名称体系结构(Jtree1 arch。)并且由于需要具有JTree2体系结构而无法读取。我有函数changeAttributesNamesFromDOORSToTC()来解决问题,因为它只是将名称更改为正确的名称,并且可以理解为JTree2

真正的问题:该函数在JTree2的节点中进行更改,但同时更改了JTree1中同一节点的值名称。它创建参考数据而不是我认为的任务。

我该如何解决这个问题??

谢谢!

JTree treeDOORSCode; //JTree1
JTree treeTCCode;  //JTree2

主要代码:

//ACTUAL NODE
DefaultMutableTreeNode selectedTreeNode = (DefaultMutableTreeNode) CurrentSelection.getLastPathComponent();
NodeClass actualNode = (NodeClass)selectedTreeNode.getUserObject();


//ACTUAL PARENT NODE
DefaultMutableTreeNode selectedParentTreeNode = (DefaultMutableTreeNode)     selectedTreeNode.getParent();
NodeClass parentNode = (NodeClass) selectedParentTreeNode.getUserObject();
DefaultMutableTreeNode parent = findNode(NodeClass.getNodeParentIdentifierAttrDOORS(parentNode), treeTCCode);


//NEW NODE
DefaultMutableTreeNode newSelectedTreeNode = selectedTreeNode;

//NEW PART
NodeClass newNode = new NodeClass();
        newNode = insertNodeInfo(actualNode);

//Create the Model and insert the node
DefaultTreeModel treeModelTC = (DefaultTreeModel)treeTCCode.getModel();
treeModelTC.insertNodeInto(newSelectedTreeNode, parent, 0);

//NEW PART
newNode .changeAttributesNamesFromDOORSToTC();
newSelectedTreeNode.setUserObject(newNode);

更改attr Name值的函数:

public void changeAttributesNamesFromDOORSToTC(){

    for (int i = 0; i < this.attributes.size(); i++) {
        if (this.attributes.get(i).attributeName.equals(DOORS_ID)){
            if (this.tag.equals(TYPE_NAME_CASE)){
                this.attributes.get(i).attributeName = TC_IDCASE;
            }
            if (this.tag.equals(TYPE_NAME_FOLDER)){
                this.attributes.get(i).attributeName = TC_IDFOLDER;
            }
            if (this.tag.equals(TYPE_NAME_FEATURE)){
                this.attributes.get(i).attributeName = TC_IDFEATURE;
            }
        }
        if (this.attributes.get(i).attributeName.equals(DOORS_PARENTID)){
            this.attributes.get(i).attributeName = TC_PARENTID;
        }
        if (this.attributes.get(i).attributeName.equals(DOORS_SRS)){
            this.attributes.get(i).attributeName = TC_SRS;
        }

    }
}

属性类:

NodeAttributesClass (String attributeName, String attributeValue)
{
    this.attributeName = attributeName;
    this.attributeValue = attributeValue;
}

如果需要更多信息,请告诉我们!

1 个答案:

答案 0 :(得分:0)

java中的对象赋值实际上是一个引用副本。

NodeClass newActualNode = actualNode;

该行并不意味着“将对象的实际节点的值放入对象newActualNode”,因为实际上变量actualNode不是NodeClass的实例,而是引用到NodeClass的实例。因此,当您执行NodeClass newActualNode = actualNode;时,您正在复制引用,现在两个变量都有效地指向同一个实例。

然后当你在一个“另一个”中更改属性名称时也会发生变化,因为没有其他内容,它在内存中是相同的位置。

现在,您需要做的是使用您想要的值创建NodeClass的新实例。有几种方法可以做到这一点,我很难知道哪一种更合适,因为我不知道内部结构,但最后你需要:

  • 为newActualNode变量
  • 创建一个新的NodeClass实例
  • 将actualNode的值(字段)放入newActualNode
  • 在newActualNode
  • 中指定所需的属性名称

所以,它可能是这样的:

NodeClass newActualNode = new NodeClass(actualNode);  //copy constructor  NodeClass(NodeClass anotherNode);
newActualNode.changeAttributesNamesFromDOORSToTC(); //assuming the constructor doesn't put them right

或者您可以在NodeClass构造函数中使用标记来指示您想要的属性名称类型。

并且复制构造函数应如下所示:

public NodeClass(NodeClass anotherNode)
{
    this(anotherNode.someFields); //call to your "normal" constructor, with whatever params you need

    //copy the values into the new instance this, if you didn't do it in the above line
    this.field1 = anotherNode.field1;
    this.field2 = anotherNode.field2;
    //...
    this.fieldn = anotherNode.fieldn;
}

你可以使用这些代码,有几种方法可以做到这一点,差异是微妙的,如果有的话。重要的是你需要为另一棵树提供另一个实例。

修改

如果这不起作用我的猜测是,你需要在插入之前做newSelectedTreeNode.setUserObject(newNode);,如下所示:

NodeClass newNode = new NodeClass();
newNode = insertNodeInfo(actualNode);
newNode.changeAttributesNamesFromDOORSToTC();
DefaultMutableTreeNode newSelectedTreeNode = new DefaultMutableTreeNode();
newSelectedTreeNode.setUserObject(newNode);
treeModelTC.insertNodeInto(newSelectedTreeNode, parent, 0);

parent未正确计算,特别是您正在获取treeDOORSCode的父节点,而不是treeTCCode的父节点。