In my application I have 3 combo-boxes, each containing the same list of items, however each can have a different item selected. There is also a button that allows a new item to be added, when it is added it should be added to all three comboboxes, this newly added item should also become the selected item for combox 3, but the items selected for combobox 1 and 2 should not change.
I’m trying to decide whether all three combo-boxes should share the same DefaultComboBoxModel. This would make it easy for the newly added item to appear in each combo, but unlike JList there is not a separate list selection model which would seem to scupper it.
However Javadocs for JComboBoxModel says:
The selected item may not necessarily be managed by the underlying
ListModel. This disjoint behavior allows for the temporary storage and
retrieval of a selected item in the model.
So does that mean I can use a different mechanism for the selected item, I’m unclear on what the above sentence mean.
The only methods that the
JComoboBoxModelintroduces isgetSelectedItem()andsetSelectedItem(Object anItem), so I would say no.Basically, you could place the data for each model into a single, shared list. You could then create three new instances of a combo box model (either using your own implementation or seeding a
DefaultComboBoxModel) that uses this data list. These would then be applied to each combo box separately, so that each combo box had it’s own model.MyComboBoxModel
You can just as easily use an array if that’s what you’ve got