I am building a desktop app using javafx, I am downloading a file around 500 MB using ftp. I need to show the progress bar with % while downloading is in progress. I also need to give a option to cancel a ongoing downloading process. I want the progress bar when the download button is clicked and it show the download progress and if I cancel downloading should stop.
This is my code to download file.
downloadButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
// TODO Auto-generated method stub
FTPClient ftpClient = new FTPConnection().makeConnection();
try {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
success = ftpClient.changeWorkingDirectory(PATH + preset + "/" + file_to_download + offset);
System.out.println("Download Path:-" + PATH + preset + "/" + file_to_download + offset);
if (!success) {
System.out.println("Could not changed the directory to RIBS");
return;
} else {
System.out.println("Directory changed to RIBS");
}
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
if (file.getName().contains(".zip")) {
dfile = file.getName();
}
}
DirectoryChooser dirChooser = new DirectoryChooser();
primaryStage = (Stage) tableView.getScene().getWindow();
primaryStage.setTitle("Background Processes");
File chosenDir = dirChooser.showDialog(primaryStage);
System.out.println(chosenDir.getAbsolutePath());
Task task = new Task<Void>() {
@Override
public Void call() throws IOException {
try {
output = new FileOutputStream(chosenDir.getAbsolutePath() + "/" + dfile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ftpClient.sendNoOp();
ftpClient.setConnectTimeout(1000);
int filesize = 0;
if (ftpClient.retrieveFile(dfile, output) == true) {
downloadButton.setDisable(true);
dfile.length();
System.out.println("FILE:LENGTH" + dfile.length());
}
// updateProgress(outByte, inputByte);
return null;
}
};
final Stage dialog = new Stage();
dialog.initModality(Modality.WINDOW_MODAL);
dialog.initOwner(primaryStage);
final ProgressBar progressBar = new ProgressBar(0);
final Button cancelButton = new Button("Cancel");
cancelButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
copyWorker.cancel(true);
progressBar.progressProperty().unbind();
progressBar.setProgress(0);
System.out.println("cancelled.");
dialog.close();
}
});
final Button closeButton = new Button("Close");
closeButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
progressBar.progressProperty().unbind();
progressBar.setProgress(0);
dialog.close();
}
});
cancelButton.setDisable(false);
closeButton.setDisable(true);
VBox dialogVbox = new VBox();
VBox.setMargin(progressBar, new Insets(10, 10, 10, 10));
VBox.setMargin(cancelButton, new Insets(10, 10, 10, 10));
dialogVbox.setAlignment(Pos.CENTER);
HBox hBox = new HBox();
hBox.setAlignment(Pos.CENTER);
hBox.getChildren().addAll(cancelButton, closeButton);
dialogVbox.getChildren().addAll(progressBar, hBox);
Scene dialogScene = new Scene(dialogVbox, 160, 100);
dialog.initStyle(StageStyle.UNDECORATED);
dialog.setResizable(false);
dialog.setScene(dialogScene);
dialog.setOnShown(e -> {
progressBar.setProgress(0);
copyWorker = createWorker();
progressBar.progressProperty().unbind();
progressBar.progressProperty().bind(copyWorker.progressProperty());
copyWorker.messageProperty().addListener(new ChangeListener<String>() {
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println(newValue);
}
});
// TODO: handle succeeded & failed depending on your needs
EventHandler doneHandler = new EventHandler() {
@Override
public void handle(Event event) {
cancelButton.setDisable(true);
closeButton.setDisable(false);
}
};
copyWorker.setOnSucceeded(doneHandler);
copyWorker.setOnFailed(doneHandler);
new Thread(copyWorker).start();
});
dialog.show();
// end
new Thread(task).start();
if (output != null) {
output.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
In reference to my question How to show a progress bar while downloading in javafx I have moved my download code into a different background thread but I am not able to figure out how to display the progress bar.