我创建了一个扩展 AbstractListModel 的 ComboBoxModel 类。我可以将项目添加到组合框,但是当我尝试删除时,出现异常
线程“AWT-EventQueue-0”中的异常 java.lang.IllegalArgumentException:空源
在线
this.fireIntervalRemoved(selectedItem, itemIndex, itemIndex);
public class TComboBoxModel extends AbstractListModel implements ComboBoxModel {
private int itemIndex;
private Object selectedItem = null;
private ArrayList<Object> itemList;
public TComboBoxModel() {
itemList = new ArrayList<>();
}
public void addItem(String item) {
this.itemList.add(item);
this.fireIntervalAdded(item, itemIndex, itemIndex);
}
public void removeItem() {
if (itemIndex >= 0 && itemIndex < getSize()) {
this.itemList.remove(itemIndex);
this.fireIntervalRemoved(selectedItem, itemIndex, itemIndex);
}
}
@Override
public void setSelectedItem(Object anObject) {
if ((selectedItem != null && !selectedItem.equals(anObject)) || selectedItem == null && anObject != null) {
this.selectedItem = anObject;
this.fireContentsChanged(anObject, -1, -1);
}
}
@Override
public Object getSelectedItem() {
return selectedItem;
}
@Override
public int getSize() {
return itemList.size();
}
@Override
public Object getElementAt(int index) {
return itemList.get(index).toString();
}
public int getItemIndex() {
return itemIndex;
}
public void increaseItemIndex() {
itemIndex++;
}
public void decreaseItemIndex() {
itemIndex--;
}
}