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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:33:32+00:00 2026-05-27T17:33:32+00:00

I want to change a progress bar use SetValue(int),but it doesn’t work,it always change

  • 0

I want to change a progress bar use SetValue(int),but it doesn’t work,it always change directly from 0 to 100 when progress finish.I try to create a new thread to invoke “setValue(int)”,instead of invoking form UI thread,but it’s still not worked.

my code:

public class UpdateProgressBar extends Thread{
public UpdateProgressBar(javax.swing.JProgressBar progressBar){
    this.progressBar = progressBar;
}
public void update(){
    for(int i = 1; i <= 100; i++){
         progressBar.setValue(i);
        try {
            sleep(10);
        } catch (InterruptedException ex) {
            Logger.getLogger(UpdateProgressBar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }         
}   
private javax.swing.JProgressBar progressBar;
}

,progressBar is defined in UI thread,then I UpdateProgressBar upb = new UpdateProgressBar(progressBar); in UI thread and invoke it’s update wayupb.update();did I make some mistake?

  • 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-27T17:33:33+00:00Added an answer on May 27, 2026 at 5:33 pm

    Although you have declared UpdateProgressBar to extend Thread you are not actually running it as a separate thread. You need to call start() to make the new thread actually run. If you call upb.update() from the event dispatch thread then you are executing that method on the event dispatch thread.

    You want this in your client code:

    UpdateProgressBar upb = new UpdateProgressBar(progressBar);
    upb.start();
    

    and change your UpdateProgressBar class to this:

    public class UpdateProgressBar extends Thread{
        public UpdateProgressBar(javax.swing.JProgressBar progressBar){
            this.progressBar = progressBar;
        }
    
        @Override
        public void run(){
            for(int i = 1; i <= 100; i++) {
                final int j = i;
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        progressBar.setValue(j);
                    }
                });
                try {
                    sleep(10);
                } catch (InterruptedException ex) {
                    Logger.getLogger(UpdateProgressBar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        private javax.swing.JProgressBar progressBar;
    }
    

    You need to have the SwingUtilities.invokeLater because Swing components are not thread-safe. You also need the nasty final int j = i because of Java’s less-than-brilliant handling of closures. Putting your code into the run method means it will be executed when you call Thread.start().

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

Sidebar

Related Questions

I have a progress bar that I want to change color depending on a
I want to have a progress bar which goes as a countdown from 10
I want to update a progress bar in winforms using delegate from task. scheduler
I want to Make Horizontal Progress bar Thin i want to change Internal Progress
I want to create a custom seekbar, whose progress bar's colour change between red
I am using the YUI Progress Bar but I'm having some problems with it.
I am trying to use the basic progress bar however I am unable to
i am trying to change the values of the progress bar widget to accept
in this example : http://dev.sencha.com/deploy/dev/examples/grid/progress-bar-pager.html if i want to put an delete button, after
I'm using an horizontal progress bar in my Android application, and I want to

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.