Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8499545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:41:50+00:00 2026-06-11T00:41:50+00:00

Following a previous question, which was solved, I can fill multiple comboboxes with a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:41:51+00:00Added an answer on June 11, 2026 at 12:41 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from a previous question in which I asked: How can I use
Following on from my previous question , I have now managed to rotate my
Note: I had a previous similar question which I've now attempted to delete. I
Following on from my previous question , which was so quickly answered by Meder
This is a question following my previous problem, you can find it right there
This is a followup to my previous question , which got solved (thank you
Following my previous question , in which I think I've narrowed my problem down
Following my previous question, in which I wanted to dump all the variables in
I have the following code (from a previous question on this site) which retrieves
In a previous question of mine I got the following answer, which is perfect,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.