When I call JList
and cast it to DefaultListModel
it gives me this exception.
Exception in t
I experienced this issue. I found this simple workaround:
//----instantiation----
JList mList = new JList();
mList.setModel(new DefaultListModel());
/*---- do whatever you want---- */
//Retain it wherever you want with
DefaultListModel model = (DefaultListModel)mList.getModel();
If you are using NetBeans
new DefaultListModel ()
You should not assume it is a DefaultListModel. Use the interface ListModel. The JList is returning an internal implementation of ListModel.
If you need access to the underlying model you should create it, set it in the JList constructor and retain it yourself.
Before JList<String>.getModel(),
you must initialize your object JList<String>.setModel(new DefaultModelList())