下载时如何解决JavaFX ProgressIndicator重绘问题? [英] How to solve JavaFX ProgressIndicator repainting issues while downloading?

查看:430
本文介绍了下载时如何解决JavaFX ProgressIndicator重绘问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用该循环从ftp客户端下载文件

I am downloading file from ftp client with that 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仍在一个位置。
方法setProgress成功打印该进度。

ProgressIndicator just still on one position. Method setProgress successfully priniting that progress.

public void setProgress(double value){
    System.out.println(value);
    progress.setProgress(value);
}


推荐答案

只需使用任务类,因为它意味着,无需重新发明方向盘..

Just use a Task Class as it was meant to, no need to reinvent the 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);
    }
  }
}

并绑定 progressProperty() 任务到你的 progressProperty() code> ProgressIndicator 。

and bind the progressProperty() of the Task to the progressProperty() of your ProgressIndicator.

如何绑定两个属性的示例:

Example on how to bind the two properties:

ProgressIndicator progress = new ProgressIndicator();
DownloadTask task = new DownloadTask();
progress.progressProperty().bind(task.progressProperty());
new Thread(task).start();

这篇关于下载时如何解决JavaFX ProgressIndicator重绘问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆