Search code examples
javaswingjcomboboxlistselectionlistenercomboboxmodel

populate multiple combobox with same model but select diff


Having problems with the ComboBox, I have populated multiple ComboBoxes 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);

Solution

  • 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.

    private  DefaultComboBoxModel hBoyModel= new DefaultComboBoxModel();
    private  DefaultComboBoxModel hGirlModel= new DefaultComboBoxModel();
    //....
    ComboHBoy.setModel(hBoyModel);
    ComboHGirl.setModel(hGrilModel);
    //....