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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:52:30+00:00 2026-06-03T05:52:30+00:00

I simply want to have an indeterminate JProgressBar animate in the bottom left corner

  • 0

I simply want to have an indeterminate JProgressBar animate in the bottom left corner of my frame when a long download is being done.

I’ve looked through many tutorials, none of which are clear to me. I simply want to have it animate while the file is being downloaded in the background. Each way I’ve tried this, it doesn’t animate the progress bar until after the download is done.

I need help knowing where to place my download() call.

class MyFunClass extends JFrame {
  JProgressBar progressBar = new JProgressBar();

  public void buttonClicked() {
    progressBar.setVisible(true);      
    progressBar.setIndeterminate(true);

    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        progressBar.setIndeterminate(true);
        progressBar.setVisible(true);

        // Do I do my download() in here??

    }});

    // Do download() here???
    progressBar.setVisible(false);
  }
}

Thanks in advance!


Solution
========

Edit: For those who have a similar issue to me in the future, this is the basic solution for a basic problem. This isn’t my code verbatim, but a general sketch. Inside buttonClicked():

public void buttonClicked() {
  class MyWorker extends SwingWorker(String, Object) {
     protected String doInBackground() {
       progressBar.setVisible(true);      <-- should be on the EDT
       progressBar.setIndeterminate(true);<-- should be on the EDT

       // Do my downloading code
       return "Done."
     }

     protected void done() {
        progressBar.setVisible(false)
     }
  }

  new MyWorker().execute();

}
  • 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-03T05:52:32+00:00Added an answer on June 3, 2026 at 5:52 am

    Your current code shows no creation of a background thread, but rather it shows you trying to queue code on the Swing thread from within the Swing thread which doesn’t make sense for this problem (although there are occasional times when you may want to do this, but again, not here). The only way for this to succeed is to use a background thread. The standard Oracle JProgressBar tutorial and Concurrency in Swing goes through all of this.

    The basic thing is that you must update the JProgressBar from the Swing Thread will doing your long-running process in the background thread, such as that provided by a SwingWorker object. There are too many details for us to review all here, and so all I can do is provide a link, but we’ll be happy to help you understand the details once you review the tutorials. Just check the tutorials and come on back with your specific questions if you’re still stuck.

    Edit 1
    You state:

    can I just create a new thread object within the buttonClicked() function?

    Yes, you can create a SwingWorker object inside of the buttonClicked() method and execute it there.

    The thing is I have my API and library of all the functionality that I’m developing the GUI to, and it seems like a longwinded workaround to wrap that function call in a thread.

    Sorry, but I have no idea what you’re saying here or what issues you think threading will cause. The buttonClicked() method likely must run on the EDT and not in a background thread.

    Also note that in most of my more complex Swing GUI’s, I often do my file downloading in a different (model) object and create my SwingWorker in a different object still (control) from the GUI object (the view). It may seem more complicated to do it this way, but it’s a lot easier to debug, maintain and enhance my program when I do it this way, especially when I heavily use interfaces to allow me to test all program components in isolation.

    Edit 2
    Some corrections to your solution post. You posted:

    public void buttonClicked() {
      class MyWorker extends SwingWorker(String, Object) {
         protected String runInBackground() {
           progressBar.setVisible(true);
           progressBar.setIndeterminate(true);
    
           // ...
    

    which has problems

    • it’s doInBackground(), not runInBackground()
    • but more importantly, you’re making Swing calls from within a background thread, something that should never be done (unless the call is thread safe, and even then…).

    So change it:

    public void buttonClicked() {
      progressBar.setVisible(true);
      progressBar.setIndeterminate(true);
      class MyWorker extends SwingWorker<String, Void> {
         protected String doInBackground() {
    
           // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simply put I want what http://www.reddit.com/ and http://news.ycombinator.com/ have to the left of every
I have several sql queries that I simply want to fire at the database.
Hey guys I have an assigned array from mysql results and I simply want
I have a blog on something.wordpress.com , I simply want one post to ALWAYS
I have an Activity which uses a Fragment . I simply want to pass
I have an order as an XDocument and I simply want to stick it
I have an array and simply want to get the element at index 1.
I have a small number of static sites where I simply want to hide
I must have misunderstood something. I simply want to use performSelector: to call one
I have simllar question like here: static-method-invocation , but in PHP. Simply, I want

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.