Results 1 to 9 of 9
  1. #1
    Jedan_covik is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Running swingworker more than once

    I need to run swingworker class more than once. Creating new instance of that class does work but GUI is updated only when the thread is done. Is there a way to fix it?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Running swingworker more than once

    Quote Originally Posted by Jedan_covik View Post
    I need to run swingworker class more than once. Creating new instance of that class does work but GUI is updated only when the thread is done. Is there a way to fix it?
    Please have a look at the SwingWorker API, and you'll see that it should only be used once and that's it. You will have to address the problem the other way: fix the SwingWorker so that it works correctly if new instances are made. I'm not sure what it's doing wrong based on your current description. You can update the GUI as the SwingWorker is running via its publish/process method pairs or by adding a PropertyChangeListener to the SwingWorker.

    If you give us more information and code, I'll bet that we can get you better help.
    Last edited by Fubarable; 09-01-2012 at 01:30 PM.

  3. #3
    Jedan_covik is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Running swingworker more than once

    So my first instance of class load_images works properly and updates GUI properly. When i create a new instance of that class GUI is updated when the loop is finished.

    From oracles docs i got this :"Because the process method is invoked asynchronously on the Event Dispatch Thread multiple invocations to the publish method might occur before the process method is executed". It got me confused... why does publish method of first instance work for each new image but when i create a new instance of the class it publishes only when done. I read that creating a new instance should get it working on another background thread, but i doesnt since GUI becomes uresponsive while new instance is running. What am i getting wrong?

    Java Code:
     public class load_images extends SwingWorker<Void,StoreImages>
       {                         
                @Override
                protected Void doInBackground() throws IOException, InterruptedException {
                             StoreImages novi=null;                //This is where i store images
                             for(int i=kraj;i<brojac;i++)
                                 {   
                                    novi = new StoreImages();
                                    File path = new File(ime.imena.get(i).toString());   //ime.imena is a list of image paths
                                    novi.Store(path, null, i+1);
                                    Thread.sleep(20);
                                    publish(novi);
                                    if(i==0)
                                        photographLabel.setIcon(new ImageIcon(novi.temp));
                                if(novi.temp!= null)
                                    kraj++;
                              }
              return null;
            }
         
          
            
            @Override
           protected void process(List<StoreImages> chunks){
                for (StoreImages novi : chunks) {
                    JButton thumbButton = null;
                    if(novi != null)
                    {
                    thumbButton = new JButton(novi);
                   }
                    else 
                    {thumbButton = new JButton();}
                    buttonBar.add(thumbButton, buttonBar.getComponentCount()-1);
                    revalidate();
                }    
          };
       }
    Last edited by Jedan_covik; 09-02-2012 at 11:57 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Running swingworker more than once

    It could be that your SwingWorker doesn't have a valid reference to the GUI. Where do you pass the reference to the GUI into the SwingWorker? Is there a constructor that we haven't seen? Also, you'll want your code to adhere to Java conventions, meaning class names should begin with an upper case letter while methods and variables with lower case letters. This will make it much easier for others (us!) to understand your code.

  5. #5
    Jedan_covik is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Running swingworker more than once

    Sorry for that, I'm new here and I forgot to read the rules. Thanks for the advice.

    Buttons are added to the GUI using this:

    Java Code:
    buttonBar.add(thumbButton, buttonBar.getComponentCount()-1);
    it is at the end of the process method. buttonBar is JPanel.

    Edit: I can't edit the code above :(

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Running swingworker more than once

    I still don't see how you're SwingWorker is getting its reference to the GUI.

  7. #7
    Jedan_covik is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Running swingworker more than once

    I did a stupid thing here actually...but at the time it didn't matter.
    buttonBar is a global JPanel, so i don't pass the reference to the class itself(which i didn't know how to do since the class extends swingworker, as you can see i'm quiet new to java :( ).

    buttonBar is a panel where buttons are represented and should be seen.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Running swingworker more than once

    Global? There are no "globals" in Java. I hope you don't mean that it was static.

  9. #9
    Jedan_covik is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: Running swingworker more than once

    No. It is defined in main class of the program(where everything is), so every class or part of the code can access it.

Similar Threads

  1. When do I use SwingWorker?
    By TacoManStan in forum AWT / Swing
    Replies: 8
    Last Post: 10-24-2011, 12:40 AM
  2. SwingWorker Explination
    By aadem in forum Advanced Java
    Replies: 4
    Last Post: 03-22-2011, 05:34 PM
  3. SwingWorker
    By 3.14.TR in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-14-2011, 04:53 PM
  4. Using SwingWorker
    By viking90 in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 09:17 AM
  5. swingworker
    By musiigedeo in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 12:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •