My JavaFX app on start shows a progress modal window in which attempts to download a file from a server. Upon successful download, my progress modal window closes and shows the main window. In case of failure to download file, an alert window is shown with a button to retry the download.
My issue is cannot find a way to retry the download when the retry button is clicked in an alert window. Pretty much any function I put in the if-else relevant to retry option is executed, except re-run the task.
public class progressController implements Initializable {
public KiteDataKit kdk;
private Thread downloadThread;
Task<String> work = DoWorkNow();
@Override
public void initialize(URL location, ResourceBundle resources) {
progressBar.progressProperty().bind(work.progressProperty());
}
public void startDownload()
{
downloadThread = new Thread(work);
downloadThread.start();
work.setOnSucceeded((e) -> {
if (work.getValue() == "success") {
masterOverviewController.populateVBox();
closeWindow();
}
else if(work.getValue() == "retry"){
printrandom(); //this gets printed
alternateDownload(); //this doesnot work
}
});
}
public void printrandom(){
System.out.println("sdfsdfsdfsd");
}
public void alternateDownload(){
Thread downloadThread1 = new Thread(work);
downloadThread1.start();
}
public Task<String> DoWorkNow() {
return new Task<>() {
@Override
protected String call() throws ExecutionException, InterruptedException {
String result;
kdk.getInstrumentList();
if (kdk.downloader.instrumentDownloaded) {
result = "success";
}
else {
Platform.runLater(showAlert);
if ((boolean) showAlert.get()) {
printrandom();
result = "retry";
}
else {
result = "exit";
}
}
return result;
}
@Override
public boolean cancel(boolean stopDownload) {
System.out.println("Cancelled");
return super.cancel(stopDownload);
}
final FutureTask showAlert = new FutureTask(() -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.getButtonTypes().clear();
alert.getButtonTypes().add(new ButtonType("Retry", ButtonBar.ButtonData.OK_DONE));
alert.getButtonTypes().add(new ButtonType("Exit", ButtonBar.ButtonData.CANCEL_CLOSE));
Optional<ButtonType> result = alert.showAndWait();
if (result.get().getText() == "Retry") {
System.out.println("Selected retry."); //this works
return true;
} else {
System.out.println(result.get());
System.out.println("requested exit app"); //this works
return false;
}
});
};
}