It does not show Alert when it is inside a CompletableFuture (JAVA)

0

I have the following code snippet:

Alert alert;
String resultado;

resultado = "incompleto";
CompletableFuture <String> futureSupplyAsync = CompletableFuture.supplyAsync(() -> {
    Future <String> future = executor.submit(new MiMetodo());
    try {
        resultado = future.get();
    } catch (Exception ex) {
        resultado = "incompleto";
    }
    return resultado;
}, executor);

futureSupplyAsync.whenCompleteAsync((s, e) -> {
    executor.shutdown();
    if (resultado == "completo") {
        System.out.println("completo todo ok");
        alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Notificación");
        alert.setHeaderText(null);
        alert.setContentText("Finalizo el proceso correctamente");
        alert.showAndWait();
    } else {
        System.out.println("paso un error");
        alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("Atencion");
        alert.setHeaderText("Hubo un error al procesar informacion.");
        alert.setContentText("Hubo un error");
        alert.showAndWait();
    }
});

The code works, when it evaluates resultado of true , and by console it is "complete all ok", but the alert does not show it. What could be happening to my code?

    
asked by Paulo Ariel 11.07.2018 в 22:14
source

1 answer

0

I do not know the class CompletableFuture but I suspect that it may be a problem with the threads.

You can put the alert block in a try-catch and use e.printStackTrace(); (only), to see details of the problem.

Replace the line:

alert.showAndWait();

for this one:

Platform.runLater(alert::showAndWait);

You can consult the following answer (in English) that talks about problems with Alert in multi-threaded environments: link

    
answered by 05.09.2018 в 10:00