snajalm
asked on
Casting from a ListModel into a DefaultListModel in Java 7
How can I cast a ListModel to a DefaultListModel??! I perform the following and I get this error message. Please keep in mind that I'm using JDK5 where they introduced a new concept "generics". I'm a bit confused as how this should replace the old down casting routine. Please help me resolve my confusion.
I have already read these topics and can understand where they come from but have difficulty implementing this in my code.
http://download.oracle.com/javase/tutorial/extra/generics/index.html
I have already read these topics and can understand where they come from but have difficulty implementing this in my code.
http://download.oracle.com/javase/tutorial/extra/generics/index.html
DefaultListModel portalListModel = (DefaultListModel) portalList.getModel();
C:\Documents and Settings\User\MyDocuments\NetBeansProjects\iDA\iDA\src\ida\resources\NewPortalConnection.java:728: warning: [rawtypes] found raw type: DefaultListModel
DefaultListModel portalListModel = (DefaultListModel) portalList.getModel(); missing type arguments for generic class DefaultListModel<E>
where E is a type-variable:
E extends Object declared in class DefaultListModel
ASKER
I tried this,
but it comes up with the following erroe,
DefaultListModel<E> portalListModel = (DefaultListModel) portalList.getModel();
but it comes up with the following erroe,
C:\Documents and Settings\somefile.java:728: warning: [unchecked] unchecked conversion
DefaultListModel<E> portalListModel = (DefaultListModel) portalList.getModel();
required: DefaultListModel<E>
found: DefaultListModel
If you say
DefaultListModel<Object> portalListModel = (DefaultListModel) portalList.getModel();
would not it work ?
I don't have Java 7 , so I cannot test.
But you realize that generics or not generics you cannot cast any ListModel to DefaultListModel - it may throw runtime exception if this instance is not instacne of
DefaultListModel
But you realize that generics or not generics you cannot cast any ListModel to DefaultListModel - it may throw runtime exception if this instance is not instacne of
DefaultListModel
ASKER
Here's what I get from replacing your code with mine!
C:\Documents and Settings\somefile.java:728: warning: [unchecked] unchecked conversion
DefaultListModel<Object> portalListModel = (DefaultListModel) portalList.getModel();
required: DefaultListModel<Object>
found: DefaultListModel
this is a warning - try to execute it.
ASKER
Can you please help me understand these generics and get it right together??! I really nead help with this! My code is fine except these "warning: [rawtypes] found raw type: DefaultListModel" warnings!
Don't worry about the warnings - are you sure that your cast will work?
Can you show the code how you created the model for that list ?
Can you show the code how you created the model for that list ?
try if this works without error on Java 7
ListModel portalListModel = (DefaultListModel) portalList.getModel();
You probaly need to create JList with generics
JList<String> portalList = new JList<String>();
DefaultListModel<Srtring> portalListModel = (DefaultListModel) portalList.getModel();
JList<String> portalList = new JList<String>();
DefaultListModel<Srtring> portalListModel = (DefaultListModel) portalList.getModel();
because in JDK 7 JList suppoerts generiocs
So like with collections in oprder to avoid warnings you should
create generics objcets specifyoing the type like this:
JList<String> portalList = new JList<String>();
Then get Model returns model with generics
ListModel<String> lm = prortalList.getModel();
and probably in this case
DefaultListModel<Srtring> portalListModel = (DefaultListModel) portalList.getModel();
will not generate error
You'll still experience problems on excution, if your list model was not the instance of DefaultListModel
And those will be real errors because you cannot make this cast in all cases
as we discussed yesterday
So like with collections in oprder to avoid warnings you should
create generics objcets specifyoing the type like this:
JList<String> portalList = new JList<String>();
Then get Model returns model with generics
ListModel<String> lm = prortalList.getModel();
and probably in this case
DefaultListModel<Srtring> portalListModel = (DefaultListModel) portalList.getModel();
will not generate error
You'll still experience problems on excution, if your list model was not the instance of DefaultListModel
And those will be real errors because you cannot make this cast in all cases
as we discussed yesterday
or even so:
DefaultListModel<Srtring> portalListModel = (DefaultListModel<String>)
Yes, try like thos:
DefaultListModel<Object> portalListModel = (DefaultListModel<Object>)
ASKER
Thanks for_yan, I'll get back to this question a little later on. I've decided to ignore the warning as you initially recommended but have another very crucial question which needs to be fixed for me to be able to initiate my testing on the server!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I still didn't get the time to accurately see how to remedy this problem! My application works fine!
ASKER
http://download.oracle.com/javase/7/docs/api/javax/swing/DefaultListModel.html