循环所有JTree节点并获取每个节点中的对象

时间:2015-05-03 07:15:42

标签: java swing jtree classcastexception

我有JTree
enter image description here

我已插入每个节点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());
}

0 个答案:

没有答案