问题
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