Question

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);
Was it helpful?

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);
//....

OTHER TIPS

Use just a ListModel to manage your data and create a ComboboxModel adapter that is based on the ListModel. This ComboboxModel will only add the selection capability. Remember that a ComboboxModel extends ListModel. So it is easy to adapt the interfaces.

The only tricky part is to handle the update events.

For example:

public class ListAdapterComboboxModel implements ComboBoxModel {

    private ListModel dataModel;
    private Object selectedObject;
    private DataModelListDataListenerAdapter listDataListenerAdapter;

    public ListAdapterComboboxModel(ListModel ListModel) {
        dataModel = ListModel;
        this.listDataListenerAdapter = new DataModelListDataListenerAdapter();
        dataModel.addListDataListener(listDataListenerAdapter);
    }

    public int getSize() {
        return dataModel.getSize();
    }

    public Object getElementAt(int index) {
        return dataModel.getElementAt(index);
    }

    public void addListDataListener(ListDataListener l) {
        listDataListenerAdapter.addListDataListener(l);
    }

    public void removeListDataListener(ListDataListener l) {
        listDataListenerAdapter.removeListDataListener(l);
    }

    public void setSelectedItem(Object anObject) {
        if ((selectedObject != null && !selectedObject.equals(anObject))
                || selectedObject == null && anObject != null) {
            selectedObject = anObject;
            ListDataEvent e = new ListDataEvent(this,
                    ListDataEvent.CONTENTS_CHANGED, -1, -1);
            listDataListenerAdapter.delegateListDataEvent(e);
        }
    }

    public Object getSelectedItem() {
        return selectedObject;
    }

    private class DataModelListDataListenerAdapter implements ListDataListener {

        protected EventListenerList listenerList = new EventListenerList();

        public void removeListDataListener(ListDataListener l) {
            listenerList.remove(ListDataListener.class, l);
        }

        public void addListDataListener(ListDataListener l) {
            listenerList.add(ListDataListener.class, l);

        }

        public void intervalAdded(ListDataEvent e) {
            delegateListDataEvent(e);
        }

        public void intervalRemoved(ListDataEvent e) {
            checkSelection(e);
            delegateListDataEvent(e);
        }

        public void contentsChanged(ListDataEvent e) {
            checkSelection(e);
            delegateListDataEvent(e);
        }

        private void checkSelection(ListDataEvent e) {
            Object selectedItem = getSelectedItem();
            ListModel listModel = (ListModel) e.getSource();
            int size = listModel.getSize();
            boolean selectedItemNoLongerExists = true;
            for (int i = 0; i < size; i++) {
                Object elementAt = listModel.getElementAt(i);
                if (elementAt != null && elementAt.equals(selectedItem)) {
                    selectedItemNoLongerExists = false;
                    break;
                }
            }
            if (selectedItemNoLongerExists) {
                ListAdapterComboboxModel.this.selectedObject = null;
            }
        }

        protected void delegateListDataEvent(ListDataEvent lde) {
            ListDataListener[] listeners = listenerList
                    .getListeners(ListDataListener.class);
            for (ListDataListener listDataListener : listeners) {
                listDataListener.contentsChanged(lde);
            }
        }

    }

}

And then just use it like this

public class ComboboxModelTest extends JFrame{

    public static void main(String[] args) {
        ComboboxModelTest comboboxModelTest = new ComboboxModelTest();
        comboboxModelTest.pack();
        comboboxModelTest.setVisible(true);
    }

    public ComboboxModelTest() {
        Container contentPane = getContentPane();
        contentPane.setLayout(new FlowLayout());

        DefaultListModel defaultListModel = new DefaultListModel();
        defaultListModel.addElement("Element 1");
        defaultListModel.addElement("Element 2");

        ComboBoxModel firstComboboxModel = new ListAdapterComboboxModel(defaultListModel);
        ComboBoxModel secondComboboxModel = new ListAdapterComboboxModel(defaultListModel);
        JComboBox jComboBox1 = new JComboBox(firstComboboxModel);
        JComboBox jComboBox2 = new JComboBox(secondComboboxModel);

        contentPane.add(jComboBox1);
        contentPane.add(jComboBox2);
    }
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top