I'm using JTree to create tree view and add node to its root as following:
String nodeName = "node1";
DefaultMutableTreeNode child = new DefaultMutableTreeNode(nodeName);
root.add(child);
The UserObject
for each node now has type of String
. It shows "node1"
as node name when display the tree.
However, I want to add UserObject
to the node as an Object of the nodeObject
class with 2 attributes:
private class nodeObject{
private String nodeName;
private boolean isSomethingElse;
public nodeObject(String name, boolean something){
nodeName = name;
isSomethingElse = something;
}
public String getName(){
return nodeName;
}
//Other setter/getter after these code
}
When I add this nodeObject to tree node:
nodeObject nodeObject = new nodeObject("node1",true);
DefaultMutableTreeNode child = new DefaultMutableTreeNode(nodeObject);
root.add(child);
It shows the object ID
as node name.
My question is, how i can set the node name as nodeObject.getName()
so the tree can show "node1"
as node name?
Any reply is much appreciated. Thank you!