My swingworker won't repaint my progress bar(I have 2 classes).
This is my file downloader code. It puts percent of download in progress bar.
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(fileTotalSize);
//System.out.print("PROCESS:" + fileTotalSize + "\n");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
This is my button code and progress bar value change method:
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/100MB.zip", ".");
downloadFile.execute();
}
public void setBar(int Value) {
DownloadBar.setValue(Value);
DownloadBar.repaint();
System.out.print("1\n");
}
"1\n" will be printed, but progress bar won't move.
Sorry for my bad english.