Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update progressbar from worker thread

I'm building a java program that downloads N elements from a server. I'd like to have a working progress bar, that shows "already downloaded elements"/N percentage. Right now, I update progressbar value by passing to the thread a reference to the GUI object, but it makes the code "ugly" ( GUI class creates the thread, that contains a reference to the GUI itself, creating a sort of loop). Is there a better way to do it?

like image 923
kaharas Avatar asked Dec 06 '25 08:12

kaharas


2 Answers

SwingWorker has some advantages in this context, as suggested in this example.

like image 72
trashgod Avatar answered Dec 07 '25 22:12

trashgod


Read the JProgressBar API and follow the link to the Swing tutorial on How to Use Progress Bars for a working example.

like image 32
camickr Avatar answered Dec 07 '25 22:12

camickr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!