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

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

snajalmAsked:
Who is Participating?
 
ksivananthCommented:
0
 
ksivananthCommented:
by default, it uses an innerclass impl for model, you need to set DefaultListModel explicitly while constructing the list!
0
 
for_yanCommented:

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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
for_yanCommented:
DefaultListModel impelments ListModel interface
but it does not mean that any iobject which implements this interface can vbe cast to DefaultListModel
0
 
for_yanCommented:
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
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
 
for_yanCommented:
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
 
for_yanCommented:

I executed the above code and it went OK through line 7, but
threw exception at line 14
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.