Share data source for two comboboxes

自闭症网瘾萝莉.ら 提交于 2019-12-02 10:57:42

问题


I have multiple comboboxes in my GUI which all need to have the data. This data will change at random so a quick way to keep all values synced is needed. I encountered DefaultComboBoxModel which actually fits quite perfect. Only thing is, that I need the comboboxes to be independent of each other - meaning: if I select a value on one, all others shouldn't change. I've done a bit of research and read the standard java tutorials but none tell me really how to achieve this with the DefaultComboBoxModel.

This example here exactly illustrates what I need: Sharing the Data Model between two JComboBoxes Except that the selectedindex shouldn't change on both when selecting one.

This Question asked already quite a similar thing, but I couldn't make out how to approach the "decoration".

Is there someway to prevent the changing or for example just use a normal array to sync the values?

Maybe someone could give me a quick smack in the face with a fish, as the solution probably is really simple...


回答1:


I think this is what Robin explained in the answer you mentionned in your post. You wrap the original combo box model into 2 separate combo box models which rely on the original one for the data elements, but implement their own selection model.

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);
            }
        });
    }
}


来源:https://stackoverflow.com/questions/16893658/share-data-source-for-two-comboboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!