populate multiple combobox with same model but select diff

后端 未结 2 1474
小鲜肉
小鲜肉 2021-01-16 07:49

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

2条回答
  •  感情败类
    2021-01-16 08:30

    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.

提交回复
热议问题