2

I want a javax.swing.ListModel be shared among multiple javax.swing.ComboBoxModels.

With the following code , when I select an item from the JComboBox,in a running program through the view, the JComboBox always shows the first item as selected.

public class DelegatedComboBoxModel<T> extends AbstractListModel<T> implements
    ComboBoxModel<T> {

  protected ListModel<T> listModel;
  protected Object selectedObject;

  ListDataListener listDataChangeDelegater = new ListDataListener() {

    @Override
    public void intervalRemoved(ListDataEvent e) {
      fireIntervalRemoved(DelegatedComboBoxModel.this, e.getIndex0(),
          e.getIndex1());
    }

    @Override
    public void intervalAdded(ListDataEvent e) {
      fireIntervalAdded(DelegatedComboBoxModel.this, e.getIndex0(),
          e.getIndex1());

    }

    @Override
    public void contentsChanged(ListDataEvent e) {
      fireContentsChanged(DelegatedComboBoxModel.this, e.getIndex0(),
          e.getIndex1());
    }
  };

  public DelegatedComboBoxModel(ListModel<T> listModel) {
    // DefaultComboBoxModel<E>
    this.listModel = listModel;
    if (listModel.getSize() > 0) {
      selectedObject = listModel.getElementAt(0);
    }
    listModel.addListDataListener(listDataChangeDelegater);
  }

  @Override
  public T getElementAt(int index) {
    if (index >= 0 && index < listModel.getSize())
      return listModel.getElementAt(index);
    else
      return null;
  }

  @Override
  public int getSize() {
    return listModel.getSize();
  }

  @Override
  public void setSelectedItem(Object anObject) {
    if ((selectedObject != null && !selectedObject.equals(anObject))
        || selectedObject == null && anObject != null) {
      selectedObject = anObject;
      fireContentsChanged(this, -1, -1);
    }
  }

  @Override
  public Object getSelectedItem() {
    return selectedObject;
  }

}

I cannot figure out what went wrong with the above code.

How to fix the code ?

(Limitation : Should not use or subclass DefaultComboBoxModel or use any third-party library.)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Smith
  • 171
  • 2
  • 8
  • did you search here for diff. variation of the AbstractListModel/MutableComboBoxModel – mKorbel Oct 16 '14 at 13:08
  • Sorrt. I cannot find any such variation of AbstractListMode/MutableComboBoxModel that uses the ListModel as the source of data. – Smith Oct 16 '14 at 13:31
  • for example [AbstractListModel](http://stackoverflow.com/search?q=%5Bjava%5D+%5Bswing%5D+AbstractListModel) and [MutableComboBoxModel](http://stackoverflow.com/search?q=%5Bjava%5D+%5Bswing%5D+MutableComboBoxModel) too lazy to search for code in SSCCE/MCVE form, search for arrays based on util.List only – mKorbel Oct 16 '14 at 13:37
  • [undeleted my post about both here](http://stackoverflow.com/a/16294927/714968), and deleted post are accessible (view/undelete) for all users with reputation >10k – mKorbel Oct 16 '14 at 13:39
  • 1
    I would replace this large `if` condition with [`Objects.equals`](http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#equals(java.lang.Object,%20java.lang.Object)), but anyway, it doesn’t look wrong to me. When I try your code with `String` items, everything works fine. Do you have some custom item type with a broken `equals` implementation? – Holger Oct 16 '14 at 13:58
  • Yes the custom type has broken equals implementation. Thanks Holger and mKorbel. – Smith Oct 16 '14 at 14:21

0 Answers0