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

The Archive Base Latest Questions

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

I made a sample to make the question clear : public class Worker extends

  • 0

I made a sample to make the question clear :

public class Worker extends SwingWorker<Integer, Integer> {
    private GeneralUserInterface gui;

    public Worker(GeneralUserInterface gui){
        this.gui = gui;
    }

    @Override
    protected Integer doInBackground() throws Exception {
        int someResultToReturn = 10;

        for(int i=0; i<100; i++){
            Thread.sleep(50);//The Work
            publish(i+1);//calls process, which updates GUI
        }

        return someResultToReturn;
    }

    @Override
    protected void process(List<Integer> values) {
        for (Integer val : values) {
            gui.updateProgressBar(val);
        }
    }
}

private void jButtonDoWorkActionPerformed(java.awt.event.ActionEvent evt) {
    Worker worker = new Worker(this);
    worker.execute();

    try {
        int resultToGet = worker.get();//Obviously freezes the GUI
    } catch (InterruptedException | ExecutionException ex) {}

    //NEXT LINE NEEDS THE RESULT TO CONTINUE
}

public void updateProgressBar(int value){
    this.jProgressBar1.setValue(value);
}

As you would guess, the call to worker.get() makes the UI unresponsive, which is normal since it waits the thread to finish. How is this kind of problem usually solved ?

  • 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-14T10:41:53+00:00Added an answer on June 14, 2026 at 10:41 am

    How is this kind of problem usually solved ?

    Normally what you do is override the Swingworker.done() method. Done is executed in the GUI thread when your background thread is completed. Then you can safely call get without blocking and do whatever you need to do.

    Here is one way to do this:

    public class Worker extends SwingWorker<Integer, Integer> {
        private GeneralUserInterface gui;
    
        // omitted...
    
        @Override
        protected Integer doInBackground() throws Exception {
            int someResultToReturn = 10;
    
            for(int i=0; i<100; i++){
                Thread.sleep(50);//The Work
                publish(i+1);//calls process, which updates GUI
            }
    
            return someResultToReturn;
        }
    
        // omitted...
    
        @Override
        protected void done() {
    
            try {
               int resultToGet = worker.get();//Obviously freezes the GUI
            } catch (InterruptedException | ExecutionException ex) {}
    
           //NEXT LINE NEEDS THE RESULT TO CONTINUE
       }
    }
    
    private void jButtonDoWorkActionPerformed(java.awt.event.ActionEvent evt) {
        Worker worker = new Worker(this);
        worker.execute();
    }
    

    However this may not be the most convenient design. I find it works best to have the GUI stuff be the public class, then create the swing worker as a non-static inner class. That way the “done” method has easy access to all the GUI private variables.

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

Sidebar

Related Questions

I think this explains my question well enough: public class Model { public static
I am trying to read this file I created as sample made up from
I hope this is a simple question. Can I inherit both an abstract class
I made a simple Test Class to simplify my actual problem : public class
Visual Studio 2008 (vb.net) I made simple anivirus but when I make Full scan
Hi guys I have made a JqueryUI simple slider,on certain value range I make
I am trying to make a simple tile system for my game. I made
I made a sample application using windows azure dedicated caching ( preview ). The
I have made a sample for posting on Facebook using the basic Facebook library
I have made a sample application which constructs a filter graph to capture audio

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.