作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 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/
我是一名优秀的程序员,十分优秀!