How to rename a node in jtree

陌路散爱 提交于 2019-12-21 23:24:04

问题


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?

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

回答1:


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.

EDIT: See 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.

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



来源:https://stackoverflow.com/questions/5685068/how-to-rename-a-node-in-jtree

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