Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: New to SwingWorker how to use process?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to SwingWorker how to use process?

    I'm new to multi-threads and am encountering an issue I can't figure out. I have built a simple class extending SwingWorker and all I want it to do is have it run a long running task in the doInBackground method. I also want to test the publish/process function so I attempted to have the process increment an int counter and display it on my GUI. Here is code:

    import java.util.concurrent.ExecutionException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.SwingWorker;
     
    public class CounterWorker extends SwingWorker<Integer, Integer> {
     
        private MainGUI gui;
        private int counter;
     
        public CounterWorker(MainGUI gui) {
     
            this.gui = gui;
            this.counter = 0;
     
        }
     
        @Override
        protected Integer doInBackground() throws Exception {
     
     
            for (int i = 0; i < 100; i++) {
     
                counter++;
                publish(counter);
                Thread.sleep(10);
            }
     
            return 10;
        }
     
        protected void process(Integer count) {
     
            int c = count;
     
            gui.setCounterLabel(Integer.toString(c));
     
     
     
     
        }
     
        @Override
        protected void done() {
     
            gui.setStatusLabel("Finished");
            try {
                gui.setMainLabel(get().toString());
            } catch (InterruptedException ex) {
                Logger.getLogger(CounterWorker.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ExecutionException ex) {
                Logger.getLogger(CounterWorker.class.getName()).log(Level.SEVERE, null, ex);
            }
     
        }
    }

    The doInBackground eventually is finished and done runs, but process never does anything (I don't see the counter). Is this even do-able? I appreciate the help.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: New to SwingWorker how to use process?

    If you put an @Override annotation above your process method, your code won't compile, and that's because its method signature is wrong and doesn't override a parent method. The method should accept a List<Integer> parameter, not an Integer parameter. You should then iterate through the List<Integer> setting your counter label with it:

       @Override  // don't forget this
       protected void process(List<Integer> chunks) {
          for (Integer chunk : chunks) {
             // no need for the c variable
             gui.setCounterLabel(chunk.toString());
          }
       }

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to SwingWorker how to use process?

    Everything makes sense now, thank you so much.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: New to SwingWorker how to use process?

    You're quite welcome!

Similar Threads

  1. SwingWorker class, the process method
    By Dirk94 in forum AWT / Java Swing
    Replies: 2
    Last Post: March 27th, 2012, 03:48 AM
  2. SwingWorker - max memory?
    By fractalorbit in forum AWT / Java Swing
    Replies: 1
    Last Post: September 15th, 2011, 02:58 PM
  3. updating EDT with thread swingworker/invokeLater ?
    By mdstrauss in forum AWT / Java Swing
    Replies: 0
    Last Post: October 11th, 2009, 04:52 AM
  4. swingworker OR a swingUtilites.invokeLater()
    By mdstrauss in forum AWT / Java Swing
    Replies: 0
    Last Post: October 11th, 2009, 04:50 AM
  5. Replies: 0
    Last Post: October 10th, 2009, 01:25 PM