warning: [rawtypes] found raw type: DefaultListModel

Sam picture Sam · Oct 19, 2011 · Viewed 11.8k times · Source

I'm getting this warning from the compiler which does not make sense at all (at least to me). It basically wants me to assign a Type to DefaultListModel which is a object type by itself! I'm getting heaps of this warnings all through my code!

C:\Documents and Settings\...\filename.java:345:warning: [rawtypes] found raw type: DefaultListModel
DefaultListModel lm = (DefaultListModel) jList_DataSetList.getModel();
missing type arguments for generic class DefaultListModel<E>
where E is a type-variable:
E extends Object declared in class DefaultListModel

This is another one which I have no idea where is comes from!

C:\Documents and Settings\...\filename.java:897: warning: [rawtypes] found raw type: JList
private javax.swing.JList jList_DataSetList;
missing type arguments for generic class JList<E>
where E is a type-variable:
E extends Object declared in class JList

Thanks in advance

Answer

JB Nizet picture JB Nizet · Oct 19, 2011

Since Java 7, DefaultListModel is a generic type, like List, Set, etc. It expects a type : DefaultListModel<SomeClass> instead of the raw DefaultListModel.

This allows to work in a more type-safe way, because you won't be able to insert a String into a list model which is supposed to contain Integer instances. And you won't have to cast to Integer when getting an element from the model.

The same is true for JList which is also now a JList of Strings or a JList of Integers, instead of being a raw JList.

Read the tutorial about generics, and have a look at the javadoc of DefaultListModel.