Вы должны создать и показать новые окна в ветке приложения FX. Вы можете запланировать выполнение кода в потоке приложений FX, отправив его на Platform.runLater(...)
. Если вам нужен фоновый поток, чтобы дождаться, пока пользователь отклонит Alert
, вы можете использовать CompletableFuture
, как в этот вопрос :
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
// ...
CompletableFuture.runAsync(() -> {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Test Connection");
alert.setHeaderText("Results:");
alert.setContentText("Connect to the database successfully!");
alert.showAndWait();
}, Platform::runLater).join();
// ...
}
};
Если ваше оповещение возвращает нужное вам значение, используйте supplyAsync(...)
и возвращайте значение из лямбда-выражения. Затем вы можете присвоить это значение результату join()
:
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
// ...
String result = CompletableFuture.supplyAsync(() -> {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Test Connection");
alert.setHeaderText("Results:");
alert.setContentText("Connect to the database successfully!");
alert.showAndWait();
// presumably here you want to return a string
// depending on the alert...
return "" ;
}, Platform::runLater).join();
// ...
}
};