Java Swing, почему не обновляется JProgressBar при обновлении через SwingWorker?

Сделал программу для логина и загрузки файла. Файл загружает, логин приделаю потом.
В этой программе есть JProgressBar, он должен изменяться на % загрузки(У меня 2 класса).
Ниже код загрузчика:
/*
     * 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();
            }
        }
    }

Код кнопки и метода изменения setValue у 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");
    }


Проблема состоит в том, что вывод текста в консоль работает, а значение setValue у JProgressBar не меняется.
  • Вопрос задан
  • 2965 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы