创建节点后,我想获取一个 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.classpublic 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);Parent;Flopsmy range;true; ;a;false;我的范围;AA,AKs,AKob;false;我的范围;66,55,44,33,22c;false;我的范围;JJ,TT,99,88
1 回答
LEATH
TA贡献1936条经验 获得超6个赞
查看您在注释中链接的代码,树的根节点实际上确实包含String
("<html><b>Ranges</b></html>") 作为其userObject
.
鉴于此,您的选择是
用周围的节点替换根节点
MyNodeUserObject
;这可能需要对该类进行一些更改才能正确处理根节点在
visitAllNodes
方法中添加一个特殊情况(instanceof MyNodeUserObject
例如,检查)确保
visitAllNodes
没有用根节点调用
添加回答
举报
0/150
提交
取消