Having problems with the ComboBox
, I have populated multiple ComboBox
es with the same model, but when I run my program and select a value from one ComboBox it selects the same value for the rest.
ComboHBoy.setModel(defaultComboBoxModel);
ComboHGirl.setModel(defaultComboBoxModel);
ComboDHBoy.setModel(defaultComboBoxModel);
ComboDHGirl.setModel(defaultComboBoxModel);
Use just a
ListModel
to manage your data and create aComboboxModel
adapter that is based on theListModel
. ThisComboboxModel
will only add the selection capability. Remember that aComboboxModel
extendsListModel
. So it is easy to adapt the interfaces.The only tricky part is to handle the update events.
For example:
And then just use it like this
Then you only have to manage the data in one ListModel and you have distinct selection models.
Also take a look at The MVC pattern and SWING.
That's because they all are referenced to the same model, any change of the model will affect the all the other combos.
There is no way to solve this except that every combobox have it's own
DefaultComboBoxModel
.