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 8405183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:47:14+00:00 2026-06-09T22:47:14+00:00

I have a little gui program that on startup reads data from an Excel

  • 0

I have a little gui program that on startup reads data from an Excel file and some of these data need to go to the relevant comboboxes. I know how to do this by using a separate SwingWorker for each combobox:

public class ExcelReader extends SwingWorker<DefaultComboBoxModel, String> {

    private final DefaultComboBoxModel model;

    // Constructor called from a panel that has a combobox
    public ExcelReader(DefaultComboBoxModel model) {
        this.model = model;
    }

    @Override
    protected DefaultComboBoxModel doInBackground() throws Exception {

        ///// code to read data from Excel file
        ///// publish(someString) used here

        return model;
    }

    @Override
    protected void process(List<String> chunks) {

        for(String row : chunks)
            model.addElement(row);
    }
}

This works fine, but how can I use one SwingWorker class to fill multiple comboboxes? This would have the benefit of reading the file just once. Whenever something that needs to go to a combobox is found the relevant combobox is updated and then the program continues reading the next row until the end of the file.

So I tried to use boolean flags for the case that I want to update 2 comboboxes in one JPanel but this doesn’t seem to work as expected. It’s also not a good solution because in the future I’m planning to update more than 2 comboboxes in more than one panels.

public class ExcelReader extends SwingWorker<DefaultComboBoxModel[], String> {

    private boolean isData1 = false;
    private final DefaultComboBoxModel model1;
    private final DefaultComboBoxModel model2;
    private final DefaultComboBoxModel[] models = new DefaultComboBoxModel[2];

    // Constructor called from a panel that has 2 comboboxes
    public ExcelReader(DefaultComboBoxModel model1, DefaultComboBoxModel model2) {
        this.model1 = model1;
        this.model2 = model2;
    }

    @Override
    protected DefaultComboBoxModel[] doInBackground() throws Exception {

        ///// some code .....
        ///// some code .....

        // If data that needs to go to combobox1 is found
        if (someExpression)
            isData1 = true;

        publish(someString);

        ///// some code .....
        ///// some code .....

        models[0] = model1;
        models[1] = model2;

        return models;
    }

    @Override
    protected void process(List<String> chunks) {

        for (String row : chunks) {
            if (isData1) {
                model1.addElement(row);
                isData1 = false;
            }
            else
                model2.addElement(row);

    }
}

So how can only one SwingWorker be used to fill many comboboxes (that might be contained in different panels)?

By the way for the above examples I’m calling ExcelReader from one of my panels (class that extends JPanel). In the first case the panel calling the worker has only one combobox, the second has 2. While the first example works fine, is this the correct way to update the comboboxes in a gui or should the worker be called from somewhere else?

  • 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-09T22:47:16+00:00Added an answer on June 9, 2026 at 10:47 pm
    • Map your models to logical names, so you can attach data from excel to a logical part of the UI.
    • Pass to the worker the UI elements you’d like to interact with (the models, in this case)
    • Go off the UI thread ASAP (in this case the action) and let the SwingWorker sync the UI updates for you.
    • Let the SwingWorker batch updates for you, while mapping them to a logical function in your UI.

    The codez:

    import java.awt.event.ActionEvent;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.swing.AbstractAction;
    import javax.swing.BoxLayout;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingWorker;
    
    public class TestPanel extends JPanel {
    
        private TestPanel() {
            super();
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    
            Map<String, DefaultComboBoxModel> map = new HashMap<String,     DefaultComboBoxModel>();
            for (int i = 0; i < 5; i++) {
                JComboBox combo = new JComboBox();
                add(combo);
                map.put("combo" + i, (DefaultComboBoxModel) combo.getModel());
            }
    
            add(new JButton(new AbstractAction("fill me") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    new MyWorker(map).run();
                }
            }));
    
        }
    
        private class MyWorker extends SwingWorker<Void, String[]> {
    
            private final Map<String, DefaultComboBoxModel> map;
    
            public MyWorker(Map<String, DefaultComboBoxModel> map) {
                this.map = map;
            }
    
            @Override
            protected Void doInBackground() throws Exception {
                for (int i = 0; i < 20; i++) {
                    String[] cell = new String[2];
                    cell[0] = "combo" + i % 5;
                    cell[1] = "value " + i;
                    publish(cell);
                }
                return null;
            }
    
            @Override
            protected void process(List<String[]> chunks) {
                for (String[] chunk : chunks) {
                    map.get(chunk[0]).addElement(chunk[1]);
                }
            }
    
        };
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the 
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            // Create and set up the window.
            JFrame frame = new JFrame("SwingWorker");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel panel = new TestPanel();
            frame.setContentPane(panel);
    
            // Display the window.
            frame.setSize(200, 300);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have little unusual problem to solve. I need some hint or links to
I have a class that can be called either from a GUI or form
I have little snippet for validatin' my form. I need help to position the
i have little dilemma, i often use data-bound controls such as Gridview in conjunction
As a project over summer while I have some downtime from Uni I am
what I want to do is the following: I have a little GUI with
I need to create what I think should be a simple GUI. I have
I want to start making a little window-based program that runs on both Linux
When I run a single-threaded program that i have written on my quad core
I am creating a GUI using Java. This GUI launches a program from the

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.