S
S
Semyon Semenych2014-04-30 18:39:08
Java
Semyon Semenych, 2014-04-30 18:39:08

Java Swing, why doesn't JProgressBar update when refreshed via SwingWorker?

I made a program for login and file upload. The file is uploading, I'll attach the login later.
This program has a JProgressBar, it should change by % loading (I have 2 classes).
Below is the loader code:

/*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package Utils;
     
    import Main.Skin;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.SwingWorker;
     
    /**
     *
     * @author LoL
     */
    public class Downloader extends SwingWorker<String, Integer> {
     
        private String fileURL, destinationDirectory;
        private int fileTotalSize;
       
        public void DownloaderF(String file, String dir) {
            this.fileURL = file;
            this.destinationDirectory = dir;
        }
       
        @Override
        protected String doInBackground() throws Exception {
            try {
                URL url = new URL(fileURL);
                HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
                String downloadedFileName = fileURL.substring(fileURL.lastIndexOf("/")+1);
                int filesize = httpConn.getContentLength();
                //int responseCode = httpConn.getResponseCode();
                byte[] buffer = new byte[4096];
                int bytesRead = 0;
                int i = 0;
                int total = 0;
                BufferedInputStream in = new BufferedInputStream(httpConn.getInputStream());
                FileOutputStream fos = new FileOutputStream(destinationDirectory + File.separator + downloadedFileName);
                BufferedOutputStream bout = new BufferedOutputStream(fos,4096);
                while ((i=in.read(buffer,0,4096))>=0) {
                    total = total + i;
                    bout.write(buffer,0,i);
                    fileTotalSize = (total * 100) / filesize;
                    publish(fileTotalSize);
                }
                bout.close();
                in.close();
            } catch(FileNotFoundException FNFE) {
                System.out.print("HTTP: 404!");
            } catch (IOException ex) {
                Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }
       
        @Override
        protected void process(List<Integer> chunks) {
            try {
                Skin barValue = new Skin();
                barValue.setBar(chunks.get(0));
                //System.out.print("PROCESS:" + fileTotalSize + "\n");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

The code of the button and the setValue change method of the JProgressBar:
private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        // Дебаг
        Downloader downloadFile = new Downloader();
        downloadFile.DownloaderF("http://ipv4.download.thinkbroadband.com/10MB.zip", ".");
        downloadFile.execute();
        /*Thread downloader = new Thread(downloadFile);
        downloader.start();*/
    }                                           
    public void setBar(int BarValue) {
        DownloadBar.setValue(BarValue);
        DownloadBar.repaint();
        System.out.print("BAR OUT: " + BarValue + "\n");
    }


The problem is that the text output to the console works, but the setValue value of the JProgressBar does not change.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question