3

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?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Achilles
  • 1,065
  • 2
  • 13
  • 29

1 Answers1

6

DefaultMutableTreeNode exists since Java 1.2, Java Generics exists since 1.5. That is why the result of the method breadthFirstEnumeration does not have a type parameter, it is a "raw" enumeration. Same for the TreeModel. You could write a parametrized TreeModel that 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.

Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50