Question

I've created a ComboBoxModel class which extends AbstractListModel. I can add item to the combobox, but when I try to remove, I get an exception

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

at line

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

}
Was it helpful?

Solution

Pass this to the fire* methods in the model. The event source is the model, not the item.

From the documentation:

source - the ListModel that changed, typically "this"

OTHER TIPS

You should probably change it to say:

if (selectedItem != null) {
    fireIntervalRemoved(this, itemIndex, itemIndex);
}

Since you can't remove an item unless you know which one to remove by having a selected item.

You are going to have to be setting the itemIndex variable appropriately too.

public void setSelectedItem(Object anObject) {
    if ((selectedItem != null && !selectedItem.equals(anObject)) || selectedItem == null && anObject != null) {
        this.selectedItem = anObject;
        this.fireContentsChanged(anObject, -1, -1);
        itemIndex = ... index in itemList where anObject is located (or -1 if not found) ...
    }
}

Thanks to @kiheru for pointing out the problem with the 1st argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top