0

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;
            }
        });
    };
}
aiyu
  • 11
  • 5
  • Please [edit] your question to include a [mcve] which can be compiled and tested by others. And explain exactly "what doesn't work". Also, don't use `==` to compare strings, use `equals()` instead, see https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Progman Apr 19 '19 at 12:07
  • An instance of `Task` can only be executed once. If you want to execute the same work again you'll need to create a new instance of your `Task` to execute. Note there are reusable workers; for instance, the `javafx.concurrent.Service` class—but that might be overkill for your use case. – Slaw Apr 19 '19 at 15:36

0 Answers0