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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:28:46+00:00 2026-05-25T23:28:46+00:00

I have a JFrame with a CardLayout set as its layout manager. This has

  • 0

I have a JFrame with a CardLayout set as its layout manager. This has two JPanel subclasses in it. One is a panel, WordsLoadingPanel, which displays the text “Loading words…” and has a JProgressBar. The other has to actually load the words. This takes a while (about 10-14 seconds for 100 words; it’s a pretty selective algorithm), so I want to assure the user that the program is still working. I made the loading algorithm in the panel fire a property change with firePropertyChange(String, int, int), and the WordsLoadingPanel is catching the change just fine – I know this because I added a listener for this event to perform a println, and it works. However, when I change the println to actually changing the JProgressBar‘s value, it doesn’t do anything. I know I’m changing the value right, because if I set the value before the algorithm starts, it works, and it works on the last iteration of the loop. I’m guessing this is because my algorithm is eating the computing power and won’t let JProgressBar update.

So, my question is: How do I make my algorithm wait for Swing (would this be the AWT Dispatching Thread?) to finish updating the progress bar before continuing? I’ve tried:

  • Thread.yield in each iteration of the loop
  • Thread.sleep(1000L) in each iteration of the loop, in a try/catch
  • putting everything in the loop in a SwingUtilities.invokeLater(Runnable)
  • putting only the CPU-intensive algorithm in a SwingUtilities.invokeLater(Runnable)

EDIT: To further support my hypothesis of the CPU-eating algorithm (sounds like a children’s story…), when I set the JProgressBar to indeterminate, it only starts moving after the algorithm finishes.

Does anyone have any suggestions?

Thanks!

  • 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-25T23:28:47+00:00Added an answer on May 25, 2026 at 11:28 pm

    To do expensive operations in background, consider using the SwingWorker class. The documentation has examples on how to use it to do tasks that interact with the user interface in a separate thread, including progress display in JProgressBars.

    If you have trouble understanding how the class works, consider:

    • SwingWorker is a generic class that takes two parameters: T, and V
    • The doInBackground method returns T and is executed in a separate thread.
    • Since Swing may only be manipulated in the Event Dispatch Thread, you may not manipulate Swing in doInBackground.
    • The process method takes a List<V> as a parameter and is called asynchronously on the Event Dispatch Thread.
    • The publish method takes V... arguments and sends them for processing in the process method.

    In conclusion:

    • T is the type of the result of the computation, if any.
    • V is the type of the data needed to manipulate the user interface.
    • Your algorithm should run entirely in doInBackground.
    • The user interface should be manipulated in the process method.
    • Your algorithm should use publish to send data to the process method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame with null layout and two one character JLabels on the
I have a JFrame with two JPanels inside. One is set on west, other
This one's a tough one - I have a JFrame that generates JTextFields. When
I have JPanel has already added to JFrame. And I have dynamically added JPanel.
I have a JFrame, in this JFrame I have a JPanel that I draw
I have a JFrame with BorderLayout as the layout manager. In the south border,
I have this logical problem regarding on how to pass value from one JFrame
So I have a JFrame , in which it has a bunch of JPanel
I have a JFrame containing a set of JPanels in a CardLayout . Each
This is the problem, I have a class MainWindow that extends JFrame in one

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.