I'm trying to remove a item from a jListBox on java 9 I keep getting a exception saying
ClassCastException: java.desktop/javax.swing.JList$1 cannot be cast to java.desktop/javax.swing.DefaultListModel
when I call DefaultListModel model = (DefaultListModel) list2.getModel();
Program ..
String[] selections = { "green", "red", "orange", "dark blue" };
JList list = new JList(names);
list.setSelectedIndex(1);
ListModel model = list.getModel();
The exception goes oiff here
((DefaultListModel) model).removeElement(0);
.. ..
Try it this way
DefaultListModel model = (DefaultListModel) list.getModel();
int selectedIndex = list.getSelectedIndex();
if (selectedIndex != -1) {
model.remove(selectedIndex);
}
or in your particular case
DefaultListModel model = (DefaultListModel) list.getModel();
model.removeElement(0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.