Link to home
Start Free TrialLog in
Avatar of snajalm
snajalm

asked on

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

Avatar of ksivananth
ksivananth
Flag of United States of America image

by default, it uses an innerclass impl for model, you need to set DefaultListModel explicitly while constructing the list!

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

ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
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
DefaultListModel impelments ListModel interface
but it does not mean that any iobject which implements this interface can vbe cast to DefaultListModel
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

Avatar of mccarl
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.
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



I executed the above code and it went OK through line 7, but
threw exception at line 14