[英]JavaFX progress bar show file upload status
i am using JavaFX (using scene builder) and i am trying to build a progressBar with a progressIndicator that will show a background file upload status. 我正在使用JavaFX(使用场景生成器),并且正在尝试使用带有progressIndicator的progressBar来构建progressBar,以显示背景文件的上传状态。
Here is my javafx controller (some initialization): 这是我的javafx控制器(一些初始化):
@FXML
private Pane uploadsStatuses;
@FXML
private ProgressBar uploadBar;
@FXML
private ProgressIndicator uploadProgressIndicator;
...
@Override
@FXML
public void initialize(URL url, ResourceBundle rb) {
...
assert uploadsStatuses != null : "fx:id=\"uploadsStatuses\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert uploadBar != null : "fx:id=\"uploadBar\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert uploadProgressIndicator != null : "fx:id=\"uploadProgressIndicator\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
...
For uploading the file i am using FTP4J library and i have already implemented a way to calculate file upload percentage: 为了上传文件,我正在使用FTP4J库,并且我已经实现了一种计算文件上传百分比的方法:
public class Ftp4jListener implements FTPDataTransferListener {
private int transfBytes=0;
private int totalBytes=0;
private long fileSize = -1;
private String fileName;
...
@Override
public void transferred(int length)
{
transfBytes+=length;
float percent = (float) transfBytes / this.fileSize;
float fPercent = percent*100;
log.info("File: " + this.fileName + " | Bytes transfered "+ transfBytes + " Percentage: " + fPercent + "%");
}
The question is how could i bound FTP4J listener with my progressBar & progressIndicator? 问题是如何将FTP4J侦听器与progressBar和progressIndicator绑定?
Either: 要么:
Rewrite your listener class to use JavaFX Properties: 重写您的侦听器类以使用JavaFX属性:
public class Ftp4jListener implements FTPDataTransferListener {
private final ReadOnlyIntegerWrapper transfBytes = new ReadOnlyIntegerWrapper();
private final ReadOnlyIntegerWrapper totalBytes = new ReadOnlyIntegerWrapper();
private final ReadOnlyLongWrapper fileSize = new ReadOnlyLongWrapper(-1);
public ReadOnlyIntegerProperty transfBytesProperty() {
return transfBytes.getReadOnlyProperty() ;
}
public int getTransfBytes() {
return transfBytesProperty().get();
}
// etc. for other two properties...
private String fileName;
...
@Override
public void transferred(int length)
{
transfBytes.set(tranfBytes.get()+length);
float percent = (float) transfBytes.get() / this.fileSize.get();
float fPercent = percent*100;
log.info("File: " + this.fileName + " | Bytes transfered "+ transfBytes.get() + " Percentage: " + fPercent + "%");
}
}
Then you can register a listener with one or more of the properties and update your progress bar: 然后,您可以使用一个或多个属性注册一个侦听器并更新进度栏:
ftp4jListener.transfBytesProperty().addListener((obs, oldValue, newValue) ->
uploadBar.setProgress(((double)ftp4jListener.getTransfBytes())/ftp4jListener.getFileSize()));
If you are transferring the data in a background thread (which you should be), be sure to update the progress bar on the FX Application Thread: 如果要在后台线程(应该是)中传输数据,请确保更新FX Application Thread上的进度条:
ftp4jListener.transfBytesProperty().addListener((obs, oldValue, newValue) ->
Platform.runLater(() ->
uploadBar.setProgress(((double)ftp4jListener.getTransfBytes())/ftp4jListener.getFileSize())));
or from a Task
call updateProgress(...)
, and bind the progress property of the progress bar to the progress property of the task. 或从Task
调用updateProgress(...)
,然后将进度条的progress属性绑定到任务的progress属性。
Or: 要么:
Write your listener to accept a callback: 编写您的侦听器以接受回调:
public class Ftp4jListener implements FTPDataTransferListener {
private int transfBytes=0;
private int totalBytes=0;
private long fileSize = -1;
private String fileName;
private final DoubleConsumer percentageCallback ;
public Ftp4jListener(DoubleConsumer percentageCallback) {
this.percentageCallback = percentageCallback ;
}
...
@Override
public void transferred(int length)
{
transfBytes+=length;
float percent = (float) transfBytes / this.fileSize;
float fPercent = percent*100;
percentageCallback.accept(fPercent);
log.info("File: " + this.fileName + " | Bytes transfered "+ transfBytes + " Percentage: " + fPercent + "%");
}
}
Then you can create the listener as 然后您可以将侦听器创建为
Ftp4jListener ftp4jListener = new Ftp4jListener(percent ->
Platform.runLater(() -> uploadBar.setProgress(percent)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.