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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:31:06+00:00 2026-05-23T01:31:06+00:00

I have a JFrame and following components. JButton = jButton1 Progress Bar = progressBar

  • 0

I have a JFrame and following components.

JButton = jButton1
Progress Bar = progressBar and its public static
JLabel = status and its public static

When button clicks then different statements execute. I want to update my progressbar after each statement. Here is my code

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Task pbu = new Task(25, "Step 1....");
        pbu.execute();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        pbu = new Task(50, "Step 2....");
        pbu.execute();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        pbu = new Task(75, "Step 3....");
        pbu.execute();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        pbu = new Task(100, "Done");
        pbu.execute();
    }

Here is my Task Class extended with SwingWorker

public class Task extends SwingWorker{

    private int progress;
    private String stat;

    public Task(int pr, String st) {
        progress = pr;
        stat = st;
    }

    @Override
    protected Object doInBackground() throws Exception {
        NewJFrame1.progressBar.setValue(progress);
        NewJFrame1.status.setValue(stat);
        return null;
    }

}

Any idea how can I solve this problem?

  • 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-05-23T01:31:07+00:00Added an answer on May 23, 2026 at 1:31 am
       @Override
        protected Object doInBackground() throws Exception {
            NewJFrame1.progressBar.setValue(progress);
            NewJFrame1.status.setValue(stat);
            return null;
        }
    

    You don’t want to update GUI components off of the Swing thread. SwingWorker provides hooks for doing stuff back on the Event Dispatch Thread, that’s what it’s designed for. Call the publish method from doInBackground and override process to detail with incremental updates. Or, if you only need to update the GUI when it’s finished, override the done() method.

    A very simple example:

    //...in some concrete implementation of SwingWorker<Void, Integer>
    protected Void doInBackground() throws Exception {
        //do 10% of task
        doLongOp();
        publish(10);
    
        //do another 20% of task
        doAnotherLongOp();
        publish(20);
    
        return null;
    }
    
    protected void process(List<Integer> pieces) {
        for (Integer percent : pieces ) {
           progressBar.setValue(progressBar.getValue() + percent);
        }
    }
    

    Edit

    @mKorbel makes a good point that for the above to work it needs to extend SwingWorker<Void, Integer>…Void being the overall return value type (in this case indicating there’s no return value) and Integer being the type of the incremental updates.

    If the worker produces an actual final result (that will be retrieved with get()) then that type can be used in place of Void. I had left out these details since I didn’t include the class declaration.

    See

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

Sidebar

Related Questions

I have a parent JFrame it contain a JButton . functionality of that button
I have a JFrame with a menu bar and a canvas covering all the
I have a JFrame. This JFrame contains a JButton. I click the JButton and
Following situation: I have a JFrame and call JOptionPane.showInputDialog(test) . The modal dialog will
I have a JFrame that contains a display JPanel with JTextField and a control
My current problem is that I have a JFrame with a 2x2 GridLayout. And
This one's a tough one - I have a JFrame that generates JTextFields. When
Java Newbie here. I have a JFrame that I added to my netbeans project,
I have a window (derived from JFrame) and I want to disable the close
I have a simple form setup and I was following how the book, Core

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.