Assign unique ID to a DefaultMutableTreeNode in a JTree?

岁酱吖の 提交于 2019-12-11 11:57:30

问题


I have a Jtree that I can add new Nodes to. I need to assign a unique ID to each new node. I was thinking of extending the DefaultMutableTreeNode Class but I guess it didn't work or I did it wrong.

So, how can I do this. An example would be nice. Thanks.


回答1:


My impression is that DefaultMutableTreeNode works "best" without extending it, but wrapping your own user-defined object. And that could have an ID.

Nevertheless your way should work too, when creating a nodes children with your own class. A matter of debugging, and trace logs: creation of all objects and such. Mind that one can easily err in the API and several times create TreeNode-s for a child.




回答2:


OK I figured it out and it WORKS :) I extednded DefaultMutableTreeNode:

 public class MyTreeNode extends DefaultMutableTreeNode {

    public int ID;

    public void setID(int ID) {
        this.ID = ID;
    }

    public MyTreeNode(String title) {
        setUserObject(title);
    }
}


来源:https://stackoverflow.com/questions/12587688/assign-unique-id-to-a-defaultmutabletreenode-in-a-jtree

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