Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with JProgressBar (not redrawing)  RSS feed

 
Adam Altmann
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I've written a program that is transferring records from one database to another. I have a main class, a GUI builder class, and a seperate class for each table I transfer. So, the main class sets stuff up, calling the GUI class, and the GUI class then calls upon each table class to handle the transfers (You select tables to transfer with checkboxes, and hit a button to begin).

I have two JProgressBars, one that shows the progress on the current table, and one that shows total progress. So, if I want to transfer two tables (each with 1000 records) then the top bar goes to 1000, and then resets when the second table is started, while the bottom bar will go all the way to 2000.

I set the maximum values for the bars in the GUI class, and have a static method for updating the values, which is then called per record in each respective table class.

I hope this has made sense thus far...

The problem is, when I run the program, the GUI window seems to lose focus while the queries and updates run, and so the ProgressBars don't repaint. These are no small tasks, each table transferred takes a number of minutes to complete, so I'd like the user to be able to see where he stands.

Am I overlooking something obvious?

Thanks in advance.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you run your long tasks on their own threads? If so, look into SwingUtilities invokeAndWait() or invokeLater() methods. Create a runnable object that updates the progress bar and pass that object to one of those methods. I had a similar GUI that only updated the progress info if I resized the window until I found SwingUtilities.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing forum...
 
Scott Delap
author
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam,

Sounds like a Event Dispatch Thread issue. As the other post says, I'd check if you task are running on the EDT. If you check out this post from a few days ago, you will see I posted some information on how to do this.

http://www.coderanch.com/t/340901/GUI/java/Swing-Performance-slow-swing

If nothing else you might have to manually call revalidate()/repaint() when the progress bar is updated.

Scott Delap
ClientJava.com
Desktop Java Live
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!