I have been working on setting a unique Id for the DefaultMutableTreeNode. One method is to create a simple CustomUserObject Class which has tow properties, Id and Title. We can then assign the CustomUserObject instance as the Node UserObject property.
To make sure that only the Title is displayed in the Tree structure, override the toString() method in the CustomUserObject Class.
/* CustomUserObjectClass */
public class CustomUserObject implements Serializable {
private int Id = 0;
private String Title = null;
public CustomUserObject(int id, String title) {
this.Id = id;
this.Title = title;
}
public CustomUserObject() {
}
public int getId() {
return this.Id;
}
public String getTitle() {
return this.Title;
}
public void setId(int id) {
this.Id = id;
}
public void setSutTitle(String title) {
this.sutTitle = title;
}
@Override
public String toString() {
return this.Title;
}
Now to create a new node:
CustomUserObject uObj = new CustomUserObject(1, "My First Node");
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(uObj);
uObj = childNode.getUserObject();
uObj.getId();
uObj.getTitle();