After I created a node I'd like to get a MyNodeUserObject so I can call the getFlops() method later. But I can't cast the userobject to MyNodeUserObject.
Creating the node:
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;
}
Content of tree.txt:
Node name;IsFolder(true/false);Parent;Flops
my ranges;true; ;
a;false;my ranges;AA,AKs,AKo
b;false;my ranges;66,55,44,33,22
c;false;my ranges;JJ,TT,99,88
Looking at the code you linked in a comment, the root node of your tree actually does contain a String
("<html><b>Ranges</b></html>") as its userObject
.
Given that, your options are
MyNodeUserObject
; this probably necessitates some changes to that class to handle the root node correctlyvisitAllNodes
method (checking for instanceof MyNodeUserObject
, for example)visitAllNodes
is not called with the root node