?
Solved

Casting from a ListModel into a DefaultListModel in Java 7

Posted on 2011-10-19
17
Medium Priority
?
1,763 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:snajalm
  • 10
  • 6
17 Comments
 

Author Comment

by:snajalm
ID: 36997891
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
0
 

Author Comment

by:snajalm
ID: 36997901
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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36997940

If you say
DefaultListModel<Object> portalListModel = (DefaultListModel) portalList.getModel();
would not it work ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36997946
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
0
 

Author Comment

by:snajalm
ID: 36997961
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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36997967
this is a warning - try to execute it.
0
 

Author Comment

by:snajalm
ID: 36997970
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!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36997977
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 ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36997994

try if this works without error on Java 7
ListModel portalListModel = (DefaultListModel) portalList.getModel();

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36998006
You probaly need to create JList with generics

JList<String> portalList = new JList<String>();

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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36998019
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





0
 
LVL 47

Expert Comment

by:for_yan
ID: 36998032

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

Expert Comment

by:for_yan
ID: 36998034

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

Author Comment

by:snajalm
ID: 36998222
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!
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36998272
Sure, this is not that dramatic, as it is indeed warning. I hope this should eliminate the warning:
DefaultListModel<Object> portalListModel = (DefaultListModel<Object>) portalList.getModel();
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 36998599
>>Casting from a ListModel into a DefaultListModel in Java 7

You can't cast into a DefaultListModel in ANY version of Java if the ListModel used by your JList is not a DefaultListModel, and in Netbeans gui builders (iirc) it isn't.

You need to apply your own model to the JList in the gui builder or (probably better) in your own code.

The Java version has no relevance to the issue
0
 

Author Closing Comment

by:snajalm
ID: 37080986
I still didn't get the time to accurately see how to remedy this problem!  My application works fine!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question