0

I'm making a small launcher for my game and I'm trying to make it download some files. Everything works, except for the progressbar and the JLabel on it. they don't update when it's downloading. Here is some of my code:

static void checkForUpdates() {
    Gui.progressBar.setIndeterminate(true);
    Gui.status.setText("Status: Checking for cache updates...");
    try {

        double newest = getNewestVersion();
        double current = getCurrentVersion();
        if(newest > current) {
            downloadCache();
            unzipCache();
            setLatestCacheVersion(newest);
        }

    } catch(Exception e) {
        e.printStackTrace();
    }
}

private static void downloadCache() throws IOException {
    Gui.progressBar.setIndeterminate(false);
    URL url = new URL(Configuration.CACHE_URL);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.addRequestProperty("User-Agent", "Mozilla/4.76");
    int responseCode = httpConn.getResponseCode();

    if (responseCode == HttpURLConnection.HTTP_OK) {
        String fileName = Configuration.CACHE_NAME;
        InputStream inputStream = httpConn.getInputStream();
        String saveFilePath = Configuration.SAVE_DIRECTORY + fileName;

        FileOutputStream outputStream = new FileOutputStream(saveFilePath);

        int bytesRead = -1;
        byte[] buffer = new byte[4096];
        long numWritten = 0;
        int length = httpConn.getContentLength();
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
            numWritten += bytesRead;
            int percentage = (int)(((double)numWritten / (double)length) * 100D);
            Gui.progressBar.setValue(percentage);
            Gui.progressBar.repaint();
        }

        outputStream.close();
        inputStream.close();

    } else {
        System.out.println("Cache host replied HTTP code: " + responseCode);
    }
    httpConn.disconnect();
}

It just keeps saying: Checking for updates, because that's what I set it to in the beginning, but after that it doesn't update.

static JLabel status = new JLabel("Checking for updates...", SwingConstants.CENTER);

I did some research and I found out that I have to do something with swingworker if I'm not mistaken. The problem is that I don't know how I would do that in this case.

RuuddR
  • 941
  • 4
  • 13
  • 25
  • I would suggest having a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) – MadProgrammer Apr 13 '17 at 22:34
  • For [example](http://stackoverflow.com/questions/15199091/progress-bar-java/15199220#15199220), [example](http://stackoverflow.com/questions/13394898/how-create-progress-bar-while-file-transfering/13395076#13395076), [example](http://stackoverflow.com/questions/15843202/how-to-show-percentage-progress-of-a-downloading-file-in-java-consolewithout-ui/15846788#15846788), [example](http://stackoverflow.com/questions/14802662/splash-screen-progress-bar-not-drawing/14803941#14803941) – MadProgrammer Apr 13 '17 at 22:37
  • [example](http://stackoverflow.com/questions/32915454/how-to-change-value-of-progress-bar-and-label/32915749#32915749), [example](http://stackoverflow.com/questions/12185924/show-progress-during-ftp-file-upload-in-a-java-applet/12186144#12186144) – MadProgrammer Apr 13 '17 at 22:37
  • Thank you for the examples, I'll see if I can get my code working. – RuuddR Apr 13 '17 at 22:39

0 Answers0