创建节点后,我想获取一个 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);Parent;Flops
my range;true; ;
a;false;我的范围;AA,AKs,AKo
b;false;我的范围;66,55,44,33,22
c;false;我的范围;JJ,TT,99,88
LEATH
相关分类