Javafx 中的 ProgressBar 在复制文件时仅显示状态 0 和 100%

ProgressBar in Javafx shows only state 0 and 100% while copying file

如何使用多线程修复此代码? 它正在工作,但我需要知道如何向这段代码添加线程,我认为这就是进度条不逐步更新的原因!

public void copyfile(ActionEvent event){

         try {

                    File fileIn = new File(filepath);
                    long length = fileIn.length();
                    long counter = 0;
                    double r;
                    double res=(counter/length);

                    filename=fieldname.getText();

                    FileInputStream from=new FileInputStream(filepath);
                    FileOutputStream to=new FileOutputStream("C:\xampp\htdocs\videos\"+filename+".mp4");
                    byte [] buffer = new byte[4096];
                    int bytesRead=0;

                    while( (r=bytesRead=from.read(buffer))!= 1){

                    progressbar.setProgress(counter/length);
                          counter += r*100;  

                    to.write(buffer, 0, bytesRead);

                    System.out.println("File is loading!!"+(counter/length));

         }

         from.close();
         to.close();
     } catch (Exception e) {
         progress.setText("upload is finished!!");
         }


     }

你能post有什么解决办法吗?

谢谢大家的建议。

Oracle's JavaFX 8 concurrency documentation.

中有一个将进度条与并发任务的进度相关联的例子
import javafx.concurrent.Task;

Task task = new Task<Void>() {
    @Override public Void call() {
        static final int max = 1000000;
        for (int i=1; i<=max; i++) {
            if (isCancelled()) {
               break;
            }
            updateProgress(i, max);
        }
        return null;
    }
};
ProgressBar bar = new ProgressBar();
bar.progressProperty().bind(task.progressProperty());
new Thread(task).start();