gpt4 book ai didi

java - ComboBoxModel - fireIntervalRemoved 的 removeItem 方法抛出异常

转载 作者:行者123 更新时间:2023-12-02 06:48:22 24 4
gpt4 key购买 nike

我创建了一个扩展 AbstractListModel 的 ComboBoxModel 类。我可以将项目添加到组合框,但是当我尝试删除时,出现异常

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: null source

在行

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--;
}

}

最佳答案

this 传递给模型中的 fire* 方法。事件源是模型,而不是项目。

来自documentation :

source - the ListModel that changed, typically "this"

关于java - ComboBoxModel - fireIntervalRemoved 的 removeItem 方法抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18345498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com