gpt4 book ai didi

java - 如何重命名jtree中的节点

转载 作者:行者123 更新时间:2023-11-30 05:04:11 24 4
gpt4 key购买 nike

我创建了一个 Jtree,它的所有节点都有 User 对象,我通过 ItemInfo 类填充用户对象,现在我的问题是重命名节点。我可以编辑我的节点,但由于我尝试找到我的 UserObject,当我按 F2 并重命名我的节点时,我看到新名称位于我的 UserObject 中!

现在我想知道我应该在哪里手动将节点的新名称替换为以前的名称?

我想我需要编写自己的 DefaultTreeCellRenderer 类,请建议我...(如果是,有任何示例供我引用)

public class ItemInfo {
public String Name;
public String Value;
public long ValueID;


public ItemInfo(String Name, String Value) {
this.Name = Name;
this.Value = Value;
}

public ItemInfo(String Name, long ValueID) {
this.Name = Name;
this.ValueID = ValueID;
}

public String toString() {
return Name;
}

public String getValue() {
return Value;
}

public long getValueID() {
return ValueID;
}
}

最佳答案

您是否按照 this 中所述添加 TreeModelListener教程?在监听器中,如果节点发生更改,您可以收到通知并更新其用户对象。

编辑:请参阅 DefaultTreeModel.valueForPathChanged javadoc:

This sets the user object of the TreeNode identified by path and posts a node changed. If you use custom user objects in the TreeModel you're going to need to subclass this and set the user object of the changed node to something meaningful.

重写此方法并使用新节点名称更新您的对象。

关于java - 如何重命名jtree中的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5685068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com