java - How to solve JavaFX ProgressIndicator repainting issues while downloading? -
i downloading file ftp client loop
private void download(string date){ try { ftpfile ftpfile = client.mlistfile(filename()); long ftpfilesize = ftpfile.getsize(); frame.setmaximumprogress((int) ftpfilesize); file downloadfile = new file(folder + filename()); outputstream outputstream2 = new bufferedoutputstream(new fileoutputstream(downloadfile)); inputstream inputstream = client.retrievefilestream(filename); byte[] bytesarray = new byte[4096]; int bytesread = -1; int bytes = 0; while ((bytesread = inputstream.read(bytesarray)) != -1){ outputstream2.write(bytesarray, 0, bytesread); bytes += bytesread; } if (client.completependingcommand()){ outputstream2.close(); inputstream.close(); } else{ joptionpane.showmessagedialog(null, "downloading error."); } } catch (ioexception e) { if(e instanceof ftpconnectionclosedexception){ reconnect(); } } }
progressindicator still on 1 position. method setprogress priniting progress.
public void setprogress(double value){ system.out.println(value); progress.setprogress(value); }
just use task class meant to, no need reinvent wheel..
public class downloadtask extends task<void> { public void call() { while ((bytesread = inputstream.read(bytesarray)) != -1){ outputstream2.write(bytesarray, 0, bytesread); bytes += bytesread; updateprogress(bytes, ftpfilesize); } } }
and bind progressproperty()
of task
progressproperty()
of progressindicator
.
example on how bind 2 properties:
progressindicator progress = new progressindicator(); downloadtask task = new downloadtask(); progress.progressproperty().bind(task.progressproperty()); new thread(task).start();
Comments
Post a Comment