?
Solved

javax.swing.JList$3 cannot be cast to javax.swing.DefaultListModel

Posted on 2011-10-17
8
Medium Priority
?
3,572 Views
Last Modified: 2012-05-12
What do you think is the problem with this statement that would render this Exception??

Following the previous question that "for_yan" responded, I have managed to get a public getter method from within the FrameView to fetch my JList, but for some reason can't cast it to DefaultListModel.

JList portalList = view.getPortalList();
DefaultListModel portalListModel = (DefaultListModel) portalList.getModel();

Open in new window


Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JList$3 cannot be cast to javax.swing.DefaultListModel

Open in new window


Here is the getter function in FrameView class,

public JList getPortalList() {
        return jList_PortalList;
}

Open in new window

0
Comment
Question by:snajalm
  • 5
  • 2
8 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 36984134
by default, it uses an innerclass impl for model, you need to set DefaultListModel explicitly while constructing the list!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36984137

potalList implements interface ListModel vbecause method
portalList.getMosdel() returns object of which we only know that it implements interface ListModel

tehre is no guarantee that this is an instance of DefaultListModle

0
 
LVL 26

Accepted Solution

by:
ksivananth earned 2000 total points
ID: 36984139
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 47

Expert Comment

by:for_yan
ID: 36984141
DefaultListModel impelments ListModel interface
but it does not mean that any iobject which implements this interface can vbe cast to DefaultListModel
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36984155
If you would have created model  of the list as insatnce of DefaultListModel and then list.setModel(new DeafultListModel)
theh indeed, when you get back ListModel of this list you would be able to cast it to DeafultListModel
but in fact you could ghave setModel of the list to object of quite different type, whcih only implements ListModel
In such case when you get back the objects which served as ListModel you would not be able to ccast to DefaultListModel

0
 
LVL 36

Expert Comment

by:mccarl
ID: 36984165
You can't cast it to DefaultListModel because it isn't an instance of DefaultListModel.

The getModel() method only returns a ListModel instance, so that is as much as you can get. ie. you can do...

 
JList portalList = view.getPortalList();
ListModel portalListModel = portalList.getModel();

Open in new window


But I am guessing you may have got that far already. So, what exactly are you doing in your code that you need access to Model as a DefaultListModel object? Can I also guess that maybe you want to update an existing static JList? If this is the case, you will have to create your own DefaultListModel and set it as the Model for the JList, and then you can handle it as a DefaultListModel.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36984188
Look at this example.

line
 DefaultListModel dflm2 = (DefaultListModel)lm2;
casues Class cast exception

but line

 DefaultListModel dflm1 = (DefaultListModel)lm;

does not cause problem,
because
ListModel for list10 was created as instance of DefaultListModle,
buty not for list20



        JList list10 = new JList();
        DefaultListModel dflm = new DefaultListModel();
        list10.setModel(dflm);

        ListModel lm = list10.getModel();

        DefaultListModel dflm1 = (DefaultListModel)lm;


        Vector v20 = new Vector();
         JList list20 = new JList(v20);
        ListModel lm2 = list20.getModel();

         DefaultListModel dflm2 = (DefaultListModel)lm2;

Open in new window


Output:
Exception in thread "main" java.lang.ClassCastException: javax.swing.JList$2 cannot be cast to javax.swing.DefaultListModel

Open in new window


0
 
LVL 47

Expert Comment

by:for_yan
ID: 36984191

I executed the above code and it went OK through line 7, but
threw exception at line 14
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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

578 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