Search code examples
javaswingswingworkerjprogressbar

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?


Solution

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