当我打电话JList<String>.getModel()
并将其转换为它时,DefaultListModel<String>
它会给我这个例外。
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JList$4 cannot be cast to javax.swing.DefaultListModel
抛出它的代码:
private JList<String> list = new JList<String>();
((DefaultListModel<String>) list.getModel()).addElement(...);
It doesn't do it every time though. Most of the time it works perfectly, but other times it throws this exception. I don't understand why this is happening. Is there anything I can do to stop this from happening?