我有JTree
:
我已插入每个节点Element
类型对象(我创建的类)
但当我想循环树以从这些节点获取对象时,控制台会向我显示:
ClassCastException (java.lang.String cannot be cast to swing.drawing.Element)
这是我的循环代码:
DefaultTreeModel model = (DefaultTreeModel) myJTree.getModel();
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) model.getRoot();
Enumeration e = parent.depthFirstEnumeration();
while(e.hasMoreElements()){
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
Element element = (Element) node.getUserObject();
System.out.println(" --> Type: "+element.getType()+" , pos in list: "+element.getPostionInList());
}