如何让两个 JComboBox 具有相同的元素?

How to have two JComboBox with the same elements?

我有一个列表 (ArrayList),其中包含我想在两个 JComboBox 中显示的元素,因此它们都显示相同的元素,但是当您在一个 JComboBox 中选择一些元素时,另一个不能更改。

我现在做的是创建两个 DefaultComboBoxModel 并在循环中添加元素。

DefaultComboBoxModel modeloA = new DefaultComboBoxModel();
DefaultComboBoxModel modeloB = new DefaultComboBoxModel();

// Agregamos el resto de plantillas.
for (OcupacionType plantilla : plantillas) {

    modeloA.addElement(plantilla);
    modeloB.addElement(plantilla);

}

comboboxA.setModel(modeloA);
comboboxB.setModel(modeloB);

这是更有效的方法吗?这是克隆模型的方法吗?

wrong suggestion start

.

  • How to have two JComboBox with the same elements? --> 没有让我明白,不要那样做,为当前 JVM 中的所有 JComboBoxes 只创建一个 DefaultComboBoxModel,并为两者共享这个模型JComboBoxes

  • 如果两个模型包含相同的数据,它们应该是低效的

  • 对于基于 AbstractListMode

  • 的可编辑 JComboBox 可能很重要

.

wrong suggestion ended

编辑来自 ListDataListener 的中断事件,它为两个实例 (JComboBox) 共享选定的项目,这里是 SSCCE/MCVE 形式的简短解释(参见对 comboBox1 & comboBox2 的选择之间的区别(使用简单的覆盖对于 ListDataListener) 与 comboBox3 & comboBox4)

.

.

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

public class SharedDataBetweenComboBoxSample {

    private final String labels[] = {"A", "B", "C", "D", "E", "F", "G"};
    private final DefaultComboBoxModel model = new DefaultComboBoxModel(labels);
    private JFrame frame = new JFrame("Shared Data");
    private JButton button = new JButton("Add");
    private JPanel panel = new JPanel(new GridLayout(2, 0, 10, 10));
    private JComboBox comboBox1 = new JComboBox(new MyComboBoxModel(model));
    private JComboBox comboBox2 = new JComboBox(new MyComboBoxModel(model));
    private JComboBox comboBox3 = new JComboBox((model));
    private JComboBox comboBox4 = new JComboBox((model));

    public SharedDataBetweenComboBoxSample() {
        comboBox1.setPrototypeDisplayValue("New Added");
        //works for editable JComboBox too
        //comboBox1.setEditable(true);
        //comboBox2.setEditable(true);
        panel.add(comboBox1);
        panel.add(comboBox2);
        panel.add(comboBox3);
        panel.add(comboBox4);
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                model.addElement("New Added");
            }
        };
        button.addActionListener(actionListener);
        frame.add(panel, BorderLayout.NORTH);
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    public static class MyComboBoxModel extends DefaultComboBoxModel
           implements ComboBoxModel, ListDataListener {

        private DefaultComboBoxModel original;

        public MyComboBoxModel(DefaultComboBoxModel original) {
            super();
            this.original = original;
        }

        @Override
        public int getSize() {
            return original.getSize();
        }

        @Override
        public Object getElementAt(int index) {
            return original.getElementAt(index);
        }

        @Override
        public void addListDataListener(ListDataListener l) {
            if (getListDataListeners().length == 0) {
                original.addListDataListener(this);
            }
            super.addListDataListener(l);
        }

        @Override
        public void removeListDataListener(ListDataListener l) {
            super.removeListDataListener(l);
            if (getListDataListeners().length == 0) {
                original.removeListDataListener(this);
            }
        }

        @Override
        public void addElement(Object anObject) {
            original.addElement(anObject);
        }

        @Override
        public void removeElement(Object anObject) {
            original.removeElement(anObject);
        }

        @Override
        public int getIndexOf(Object anObject) {
            return original.getIndexOf(anObject);
        }

        @Override
        public void insertElementAt(Object anObject, int index) {
            original.insertElementAt(anObject, index);
        }

        @Override
        public void removeAllElements() {
            original.removeAllElements();
        }

        @Override
        public void removeElementAt(int index) {
            original.removeElementAt(index);
        }

        @Override
        public void intervalAdded(ListDataEvent e) {
            fireIntervalAdded(this, e.getIndex0(), e.getIndex1());
        }

        @Override
        public void intervalRemoved(ListDataEvent e) {
            fireIntervalRemoved(this, e.getIndex0(), e.getIndex1());
        }

        @Override
        public void contentsChanged(ListDataEvent e) {
            fireContentsChanged(this, e.getIndex0(), e.getIndex1());
        }
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SharedDataBetweenComboBoxSample();
            }
        });
    }
}

正如@StanislavL 在评论中所建议的那样,您可以使用 Vector 来初始化新模型。

Vector vec = new Vector(plantillas);

comboboxA.setModel(new DefaultComboBoxModel(vec));
comboboxB.setModel(new DefaultComboBoxModel(vec));

这非常简单高效,因为只复制了一份元素(制作矢量)。