创建节点后,我想获取MyNodeUserObject,以便稍后可以调用getFlops()方法。但是我无法将userobject强制转换为MyNodeUserObject。
创建节点:
MyNodeUserObject userObject = new MyNodeUserObject("aaa","AA-10");
DefaultMutableTreeNode node = new DefaultMutableTreeNode(userObject);
public static void visitAllNodes(DefaultMutableTreeNode node, JTree tree) {
//java.lang.ClassCastException: java.lang.String cannot be cast to MyNodeUserObject
MyNodeUserObject nodeInfo = (MyNodeUserObject) node.getUserObject();
MyNodeUserObject.class
public final class MyNodeUserObject{
private static final long serialVersionUID = 1L;
private final String flops;
private final String nodeName;
public MyNodeUserObject(String nodeName, String flops) {
this.flops = flops;
this.nodeName = nodeName;
}
public String getFlops() {
return flops;
}
public String getNodeName() {
return nodeName;
}
@Override
public String toString() {
return nodeName;
}
tree.txt的内容:
节点名称; IsFolder(true / false);父级;触发器
我的范围;
a;错误;我的范围; AA,AKs,AKo
b; false;我的范围; 66,55,44,33,22
c; false;我的范围; JJ,TT,99,88
答案 0 :(得分:2)
查看您在注释中链接的代码,树的根节点实际上确实包含numexpr
(“ 范围 ”)作为其{ {1}}。
鉴于此,您的选择是
String
周围的节点替换根节点;这可能需要对该类进行一些更改才能正确处理根节点userObject
方法中添加特殊情况(例如,检查MyNodeUserObject
)visitAllNodes