我正在使用 JTree 创建树视图并将节点添加到其根,如下所示:
String nodeName = "node1";
DefaultMutableTreeNode child = new DefaultMutableTreeNode(nodeName);
root.add(child);
对于UserObject
每个节点,现在的类型为String
. 显示"node1"
树时显示为节点名称。
但是,我想作为具有 2 个属性UserObject
的类的对象添加到节点:nodeObject
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
}
当我将此 nodeObject 添加到树节点时:
nodeObject nodeObject = new nodeObject("node1",true);
DefaultMutableTreeNode child = new DefaultMutableTreeNode(nodeObject);
root.add(child);
它显示object ID
为节点名称。我的问题是,我如何设置节点名称,nodeObject.getName()
以便树可以显示"node1"
为节点名称?
任何回复都非常感谢。谢谢!