【发布时间】:2015-03-01 17:58:22
【问题描述】:
我正在使用正确显示的 JList。但是,我无法从列表中删除元素。
JList nameList = new JList(db.getAllNames());
nameList.setVisibleRowCount(6);
nameList.setFixedCellWidth(400);
JButton removeNameButton = new JButton("Remove Name");
removeNameButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String id = nameList.getSelectedValue().toString(); //valid value when button pressed
int index = nameList.getSelectedIndex(); //valid value when value pressed
namesList.remove(index); //ERROR
}
JList 包含 4 个名称,它们显示完美并且似乎具有正确的索引。 (如果我检查值 System.out.println(copiersList.getModel().getSize()); 它总是显示 4
这是错误信息:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3
奇怪的是,如果我删除 Adam,我不会收到错误消息(但显然列表没有改变并且调用 .getSize() 方法显示 4):
id selected: Adam
index selected: 0
但是,任何其他:
id selected: BobException in thread "AWT-EventQueue-0"
index selected: 1
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
at java.awt.Container.remove(Unknown Source)
【问题讨论】: