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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:56:52+00:00 2026-06-05T14:56:52+00:00

I have a Java app that uses a SwingWorker to update a label and

  • 0

I have a Java app that uses a SwingWorker to update a label and a progress bar in the GUI and it works nice. However, I’d like to add a feature to this setting.

My swing worker performs a task which has length n, and it repeats that task m times. Right now my GUI just tells me how many time the task has been repeated, but I’d like it to tell me also at what length of the task we are in. Say n=300 and m=50, I’d like something like:

Task 49 is at 248 ()
Task has been repeated 48 times

What should I modify in my SwingWorker?

/**
 *
 * @author digitaldust
 */
public class Model extends SwingWorker<Integer, Integer> {

    private HashMap<String, Number> GUIparams;
    private int session;
    private int ticks;

    Model(HashMap<String, Number> KSMParams) {
        GUIparams = KSMParams;
        session = (Integer)GUIparams.get("experimentsInSession");
        ticks = (Integer)GUIparams.get("howManyTicks");
    }

    /**
     * Actual simulation
     */
    @Override
    protected Integer doInBackground() throws Exception {

        int i=0;
        while(!isCancelled() && i<session){
            i++;
            int ii=0;
            while(!isCancelled() && ii<ticks){
                // this is n, the task length and I'd like to update the GUI with this value
                ii++;
            }
            System.out.println(i);
            // this is m, how many time the task has been repeated, and now it is updated in the GUI
            publish(i);
            setProgress(i);
            Thread.sleep(1000);
        }
        return i;
    }  

    /**
     * Invoked when simulation exits
     */
    @Override
    protected void done() {
        if (isCancelled()) {
            Logger.getLogger(Model.class.getName()).log(Level.WARNING, "Experiment session cancelled by user. Closing Session...");
        } else {
            // do stuff

            Logger.getLogger(Model.class.getName()).log(Level.WARNING, "Experiment session ended.");
        }
    }

}
  • 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-05T14:56:54+00:00Added an answer on June 5, 2026 at 2:56 pm

    The second type parameter V in SwingWorker<T,V> is used for carrying out intermediate results by this SwingWorker’s publish and process methods. This could be your custom class. Here is an example based on posted SSCCE (shortened for clarity):

    class Progress {
        private int task;
        private int element;
    
        public Progress(int task, int element) {
            super();
            this.task = task;
            this.element = element;
        }
            ...
    }
    
    public class Model extends SwingWorker<Integer, Progress> {
        ...
        @Override
        protected Integer doInBackground() throws Exception {
                ...
                publish(new Progress(i, ii));
            }
    }
    

    EDIT: example of process method implementation

    @Override
    protected void process(List<Progress> progressList) {
        for (Progress p : progressList){
            System.out.println(p.getTask() + " : " + p.getElement());
        }
    }
    

    EDIT: example of UI update

    Here is a slightly modified version of the worker implementation, similar to a sample demonstrated in SwingWorker manual. The only changes are introduction of textArea member and updated setProgress() call in doInBackground(). progress property is used to update the progress bar, process() is used to update text area.

    public static class Model extends SwingWorker<Integer, Progress> {
    
        private HashMap<String, Number> GUIparams;
        private int session;
        private int ticks;
        private JTextArea textArea;
    
        Model(HashMap<String, Number> KSMParams, JTextArea textArea) {
            GUIparams = KSMParams;
            session = (Integer)GUIparams.get("experimentsInSession");
            ticks = (Integer)GUIparams.get("howManyTicks");
    
            this.textArea = textArea;
        }
    
        @Override
        protected void process(List<Progress> progressList) {
            for (Progress p : progressList){
                textArea.append(p.getTask() + " : " + p.getElement() + "\n");
                System.out.println(p.getTask() + " : " + p.getElement());
            }
        }
    
        /**
         * Actual simulation
         */
        @Override
        protected Integer doInBackground() throws Exception {
    
            int i=0;
            while(!isCancelled() && i<session){
                i++;
                int ii=0;
                while(!isCancelled() && ii<ticks){
                    // this is n, the task length and I'd like to update the GUI with this value
                    ii++;
                }
                //System.out.println(i);
                // this is m, how many time the task has been repeated, and now it is updated in the GUI
                publish(new Progress(i, ii));
                //setProgress(i);
                setProgress(100 * i / session);
                Thread.sleep(1000);
            }
            return i;
        }  
    
        /**
         * Invoked when simulation exits
         */
        @Override
        protected void done() {
            if (isCancelled()) {
                Logger.getLogger(Model.class.getName()).log(Level.WARNING, "Experiment session cancelled by user. Closing Session...");
            } else {
                // do stuff
    
                Logger.getLogger(Model.class.getName()).log(Level.WARNING, "Experiment session ended.");
            }
        }
    }
    

    Here is a demo initialization:

    final JProgressBar progressBar = new JProgressBar(0, 100);
    final JTextArea textArea = new JTextArea();
    final JButton button = new JButton("Start");
    
    button.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            HashMap<String, Number> map = Maps.newHashMap();
            map.put("experimentsInSession", 10);
            map.put("howManyTicks", 5);
    
            Model task = new Model(map, textArea);
            task.addPropertyChangeListener(
                     new PropertyChangeListener() {
                         public  void propertyChange(PropertyChangeEvent evt) {
                             if ("progress".equals(evt.getPropertyName())) {
                                 progressBar.setValue((Integer)evt.getNewValue());
                             }
                         }
                     });
            task.execute();
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java app that uses log4j. Config: log4j.rootLogger=info, file log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File=${user.home}/logs/app.log log4j.appender.file.layout=org.apache.log4j.PatternLayout
I have a large Java app that uses massive amounts of memory at times
I have a web-app with a Java back-end that uses Tomcat jdbc-pool for database
i have a java game app that uses sockets to communicate with each other.
Some colleagues of mine have a large Java web app that uses a search
I have a small Java desktop app that uses Swing. There is a data
I have a java web start app that uses Swing and needs to allow
I have app engine app that uses a java servlet to save a message
I have a grails app that uses functionality from a set of custom java
I have a Java web application that uses a plugin architecture. I would like

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.