Search code examples
javaswinghashmapswingworker

SwingWorker updating multiple combos, lists, tables by using one map


Following a previous question, which was solved, I can fill multiple comboboxes with a SwingWorker using one HashMap. Now I want to fill/update multiple comboboxes AND lists AND tables that are contained in different panels of a gui app, by using one map (if possible). In the javadocs I saw that both DefaultListModel and DefaultComboBoxModel extend AbstractListModel, so I changed the Map from

Map<String, DefaultComboBoxModel>

to

Map<String, AbstractListModel>

which is then passed to the SwingWorker. The following code works and it can fill many combos and lists by using this one map.

SSCCE:

public class TestPanel extends JPanel {

    private final Map<String, AbstractListModel> map = new HashMap<String, AbstractListModel>();

    private TestPanel() {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        JComboBox combo1 = new JComboBox();
        JComboBox combo2 = new JComboBox();
        JList list1 = new JList(new DefaultListModel());
        JList list2 = new JList(new DefaultListModel());
        add(combo1);
        add(combo2);
        add(new JLabel("LIST 1:"));
        add(list1);
        add(new JLabel("LIST 2:"));
        add(list2);
        map.put("ComboBox1", (DefaultComboBoxModel)combo1.getModel());
        map.put("ComboBox2", (DefaultComboBoxModel)combo2.getModel());
        map.put("List1", (DefaultListModel)list1.getModel());
        map.put("List2", (DefaultListModel)list2.getModel());
        new MyWorker(map).run();
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new TestPanel());
                frame.setSize(200, 300);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private class MyWorker extends SwingWorker<Void, Object[]> {
        private final int COMBO_BOX_MODEL = 1;
        private final int LIST_MODEL = 2;
        private final Map<String, AbstractListModel> map;

        public MyWorker(Map<String, AbstractListModel> map) {
            this.map = map;
        }

        @Override
        protected Void doInBackground() throws Exception {
            // just a random way to fill the combos and the lists
            for (int i = 0; i < 20; i++) {
                Object[] cell = new Object[3];
                if (i>=0 && i<=4) {
                    cell[0] = "ComboBox1";
                    cell[1] = COMBO_BOX_MODEL;
                } else if (i>=5 && i<=9) {
                    cell[0] = "ComboBox2";
                    cell[1] = COMBO_BOX_MODEL;
                } else if (i>=10 && i<=14) {
                    cell[0] = "List1";
                    cell[1] = LIST_MODEL;
                } else if (i>=15 && i<=20) {
                    cell[0] = "List2";
                    cell[1] = LIST_MODEL;
                }
                cell[2] = "value " + i;
                publish(cell);
            }
            return null;
        }

        @Override
        protected void process(List<Object[]> chunks) {
            for (Object[] chunk : chunks) {
                if (chunk[1] == Integer.valueOf(COMBO_BOX_MODEL)) {
                    DefaultComboBoxModel model = (DefaultComboBoxModel)map.get(chunk[0]);
                    model.addElement(chunk[2]);
                }
                else if (chunk[1] == Integer.valueOf(LIST_MODEL)) {
                    DefaultListModel model = (DefaultListModel)map.get(chunk[0]);
                    model.addElement(chunk[2]);
                }
            }
        }
    };

}

Question 1: While the above code works, I don't like how it looks (in particular the checking in the process() method). Is there any better and/or more clever way to do the whole thing?

Question 2: Is there any way to make the SwingWorker to update tables too along with the combos and the lists, by using one map? I couldn't find a way since DefaultTableModel extends AbstractTableModel which is different from AbstractListModel that is used by the map.

I hope I was clear, any help would be appreciated.


Solution

    1. Your data source no doubt has a different type system than Java; the code to match those types to Java types won't be any prettier, but at least you've got it encapsulated. An enum may help.

    2. Yes, you can extend AbstractTableModel and implement the other interfaces by delegating to a default implementation. There's an outline here. You'll have to decide if such a bottleneck makes sense in your application.

      public class SharedModel extends AbstractTableModel
          implements ComboBoxModel, ListModel
      

    Addendum: The alternative is to write three models (MyTableModel, MyListModel, MyComboBoxModel) that each reference a common data model.

    Do you mean a common interface that is implemented by three models?

    Yes, an interface that is implemented by a class, say ApplicationDataModel, that reads the database into an internal format. Client models can then ask the ApplicationDataModel for the data each needs to fulfill its own interface contract.