共享两个组合框的数据源

时间:2013-06-03 09:28:37

标签: java swing jcombobox comboboxmodel

我的GUI中有多个组合框,都需要有数据。此数据将随机更改,因此需要快速保持所有值同步。我遇到了DefaultComboBoxModel,它实际上非常完美。唯一的问题是,我需要组合框彼此独立 - 这意味着:如果我在一个上选择一个值,其他所有组合都不应该改变。 我做了一些研究并阅读了标准的java教程,但没有人告诉我如何使用DefaultComboBoxModel实现这一点。

此示例在此完全说明了我的需求:Sharing the Data Model between two JComboBoxes 除了选择索引时两者都不应该改变。

This Question已经问过类似的事情,但我无法弄清楚如何接近"装饰"。

是否有某种方法可以阻止更改,或者只是使用普通数组来同步值?

也许有人可以用鱼快速打我一脸,因为解决方案可能非常简单...

1 个答案:

答案 0 :(得分:3)

我认为这就是罗宾在你的帖子中提到的答案中所解释的。您将原始组合框模型包装到2个单独的组合框模型中,这些模型依赖于原始组合框模型,但是实现了自己的选择模型。

import java.awt.BorderLayout;
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 {

    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() {
                final String labels[] = { "A", "B", "C", "D", "E", "F", "G" };

                final DefaultComboBoxModel model = new DefaultComboBoxModel(labels);

                JFrame frame = new JFrame("Shared Data");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel panel = new JPanel();
                JComboBox comboBox1 = new JComboBox(new MyComboBoxModel(model));
                comboBox1.setEditable(true);

                JComboBox comboBox2 = new JComboBox(new MyComboBoxModel(model));
                comboBox2.setEditable(true);
                panel.add(comboBox1);
                panel.add(comboBox2);
                frame.add(panel, BorderLayout.NORTH);

                JButton button = new JButton("Add");
                frame.add(button, BorderLayout.SOUTH);
                ActionListener actionListener = new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent actionEvent) {
                        model.addElement("New Added");
                    }
                };
                button.addActionListener(actionListener);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}