I am using a JTree, and I am traversing the tree using an Enumerator.
TreeModel columnTreeModel = columnTree.getModel();
TreeNode columnTreeRoot = (TreeNode) columnTreeModel.getRoot();
Enumeration<TreeNode> columnTreeEnumerator =
((DefaultMutableTreeNode) columnTreeRoot).breadthFirstEnumeration();
I get a warning in the 3rd line in this code. The warning is
The expression of type Enumeration needs unchecked conversion
to conform to Enumeration
How do I reslove this warning?
DefaultMutableTreeNodeexists since Java 1.2, Java Generics exists since 1.5. That is why the result of the methodbreadthFirstEnumerationdoes not have a type parameter, it is a “raw” enumeration. Same for theTreeModel. You could write a parametrizedTreeModelthat returns a typed root node so you wouldn’t need to cast. But it just wasn’t possible at the time Swing was designed.You can’t “resolve” this warning without changing the type (e.g. subclassing). Just set a
@SuppressWarnings("unchecked")annotation (and document why you do so) then the warning will vanish.