Question

I have a JComboBox with a custom model which extends DefaultComboBoxModel.

When I want to add an item to my combo box I add it to the model and repaint the JComboBox. However, this is leaving the internal field:

selectedItemReminder

unchanged. What should I be doing instead.

Was it helpful?

Solution

I'm not sure I understand what it is you are trying to achieve, but I might be temptered to modify the method to read more like...

private void setChildren(Collection<BoundedArea> children) {
    int oldSize = getSize();
    // Notify the listeners that the all the values have begin removed
    fireIntervalRemoved(this, 0, oldSize - 1);
    this.children.clear();
    for (BoundedArea boundedArea : children) {
        if (boundedArea.getBoundedAreaType() == childType) {
            this.children.add(boundedArea);
        }
    }
    int size = getSize();
    // Notify the listeners that a bunch of new values have begin added...
    fireIntervalAdded(this, 0, size - 1);
    setSelectedItem(null);
}

The other issue I can see is you seem to be thinking that the list is 1 based, it's not, it's 0 based, that is, the first element is 0

Updated based on changes to the question

From what I can understand, intervalAdded and contentsChanged of the JComboBox check to see if the selected value in the combo box model has changed, if it has, it calls selectedItemChanged which fires appropriate events to signal the change of the selected item...

I would, when you change the model, set the currently selected item value to something like null BEFORE you fire any event notifications...

So, using the previous example, I would do something more like...

private void setChildren(Collection<BoundedArea> children) {
    setSelectedItem(null);
    int oldSize = getSize();
    // Notify the listeners that the all the values have begin removed
    fireIntervalRemoved(this, 0, oldSize - 1);
    this.children.clear();
    for (BoundedArea boundedArea : children) {
        if (boundedArea.getBoundedAreaType() == childType) {
            this.children.add(boundedArea);
        }
    }
    int size = getSize();
    // Notify the listeners that a bunch of new values have begin added...
    fireIntervalAdded(this, 0, size - 1);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top