如何在jtree中重命名节点 [英] How to rename a node in jtree

查看:362
本文介绍了如何在jtree中重命名节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I create a Jtree that all of its nodes have User object, I fill user object by ItemInfo class, now my problem is with renaming a node. I can edit my node but since I try to find my UserObject I saw when I press F2 and rename my node, new name place in my UserObject!

所以现在我想要要知道我应该手动触摸用我以前的名字替换我的节点新名称的位置吗?

So now I want to know I should touch where to replace new name of my node with previous name manually?

我想我需要编写自己的DefaultTreeCellRenderer类,请指教我... (如果是我的参考的任何例子)

I guess I need to write my own DefaultTreeCellRenderer class, please advice me... (if yes any example for my reference)

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;
    }
}


推荐答案

TreeModelListener nofollow>这个教程?在监听器中,如果节点已更改并更新其用户对象,则会通知您。

Do you add TreeModelListener as described in this tutorial? In the listener you can be notified if a node was changed and update its user object.

编辑:请参阅DefaultTreeModel.valueForPathChanged javadoc:

See DefaultTreeModel.valueForPathChanged javadoc:


这将设置由路径标识的
TreeNode的用户对象,并更改
a节点的帖子。如果您在TreeModel中使用自定义用户
对象,那么您需要将
子类化,并将已更改节点的
用户对象设置为
有意义的内容。

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.

覆盖此方法并使用新节点名称更新对象。

Override this method and update your object with new node name.

这篇关于如何在jtree中重命名节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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