Link to home
Start Free TrialLog in
Avatar of snajalm
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

DefaultListModel portalListModel = (DefaultListModel) portalList.getModel();

Open in new window



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 

Open in new window

Avatar of snajalm
snajalm

ASKER

This is apparently the new api doc for JDK5 onwards!  I need to understand how these new generics work as a replacement to down dasting!

http://download.oracle.com/javase/7/docs/api/javax/swing/DefaultListModel.html
Avatar of snajalm

ASKER

I tried this,

DefaultListModel<E> portalListModel = (DefaultListModel) portalList.getModel();

Open in new window


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

Open in new window


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
Avatar of snajalm

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

Open in new window

this is a warning - try to execute it.
Avatar of snajalm

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 ?

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();

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






or even so:
DefaultListModel<Srtring> portalListModel = (DefaultListModel<String>) portalList.getModel();

Yes, try like thos:
DefaultListModel<Object> portalListModel = (DefaultListModel<Object>) portalList.getModel();
Avatar of snajalm

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
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of snajalm

ASKER

I still didn't get the time to accurately see how to remedy this problem!  My application works fine!