Progress bar freezing while downloading file in java [duplicate]

<strong>Possible Duplicate:</strong> JProgressBar wont update

So I am trying to show the download progress of a file being downloaded in Java. I can output the current percentage as a String to the console, but when I try to update the UI, it freezes until the download is complete.

public void downloadFile(String fileName) { try { long startTime = System.currentTimeMillis(); System.out.println("Connecting to site...\n"); URL url = new URL("http://assets.minecraft.net/"+fileName+"/minecraft.jar"); URLConnection connection = url.openConnection(); try (InputStream reader = url.openStream(); FileOutputStream writer = new FileOutputStream("minecraft.jar")) { byte[] buffer = new byte[153600]; int totalBytesRead = 0; int bytesRead = 0; int totalSize = connection.getContentLength(); jProgressBar1.setMaximum(totalSize); System.out.println("Downloading\n"); while ((bytesRead = reader.read(buffer)) > 0) { writer.write(buffer, 0, bytesRead); buffer = new byte[153600]; totalBytesRead += bytesRead; jProgressBar1.setValue(totalBytesRead); System.out.println(totalBytesRead*100/totalSize+"% complete"); } long endTime = System.currentTimeMillis(); System.out.println("Done. " + (new Integer(totalBytesRead).toString()) + " bytes read (" + (new Long(endTime - startTime).toString()) + " millseconds).\n"); } } catch (MalformedURLException e) { } catch (IOException e) { } }

I read somewhere that you need to use another thread but I was unable to successfully do that.


One option would be using SwingWorker. Here are some useful answers you may consider:


  • Maintaining custom file extension in Internet Explorer when using PHP to send file?
  • Using a WebClient to save an image with the appropriate extension
  • VBA Macro in Excel to save html files from cells range
  • How can i add a new progressBar to display overall downloads?
  • Keybinding keys to insert custom code Eclipse
  • Running a java code repeatedly for a given duration in Jenkins
  • Why does a SingleThreaded Executor service use 4 cores?
  • Getting data from calendar on Android Wear
  • detecting connection lost in spark streaming
  • New-PSSession in an Azure-runbook (ARM)
  • qt how to know that a pushbutton is clicked?
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • pillow imaging ImportError
  • Reading a file into a multidimensional array
  • Does Mobilefirst provide a provision to access web services directly?
  • how to upload multiple files in c# windows application
  • App restarts from wrong activity
  • Content-Length header not returned from Pylons response
  • Play WS (2.2.1): post/put large request
  • Jetty Server not starting: Unable to establish loopback connection
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Handling un-mapped Rest path
  • How to access EntityManager inside Entity class in EJB3
  • All Classes Conforming to Protocol Inherit Default Implementation
  • How do I fake an specific browser client when using Java's Net library?
  • Sony Xperia Z Tablet not found by adb
  • How to get a value (ex: baseURL) in every Karate feature?
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • vba code to select only visible cells in specific column except heading
  • Websockets service method fails during R startup
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Circular dependency while pushing http interceptor
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • reshape alternating columns in less time and using less memory