0

I want show an alert dialog during thread try to send a mail and I tried this code:

Alert alertPopup = new Alert(Alert.AlertType.INFORMATION);
alertPopup.setHeaderText("Sending confirmation mail!");
alertPopup.setGraphic(progressBar);
progressBar.setProgress(ProgressBar.INDETERMINATE_PROGRESS);
alertPopup.showAndWait();

MailThread MailThread = new MailThread(parameter);
Thread a = new Thread(MailThread);

a.start();

try {
    a.join();
} catch (InterruptedException e) {

    e.printStackTrace();
}
Boolean status = MailThread.getResult();
if(status){
            alertPopup.setContentText("Mail sent successfully");
            Optional<ButtonType> result = alertPopup.showAndWait();


            if(result.isPresent() && result.get() == ButtonType.OK) {
                    try {
                        onBtnBackClicked();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

but the alert dialog is not updated, 2 different dialog are shown

tecn603
  • 211
  • 5
  • 14
  • 1
    Is that all in a single thread? If so then the `alertPopup.showAndWait();` will prevent anything from happening until the dialog is closed. – Lutz Oct 22 '19 at 08:42
  • yes the popup is shown and edited by Main thread, i tried to use `alertPopup.show()` but receive this error :`java.lang.IllegalStateException: Stage already visible` – tecn603 Oct 22 '19 at 08:48
  • 1
    https://stackoverflow.com/questions/32773115/javafx-show-dialogue-after-thread-task-is-completed I think the above link can solve it. – sivaramakrishnan Oct 22 '19 at 09:21
  • A related approach is shown [here](https://stackoverflow.com/a/45718846/230513). – trashgod Oct 22 '19 at 09:42
  • `thread.start();thread.join();` is always bad idea. If you want to block the current thread until the new thread is done you could have invoked the logic on the same thread as well and used less resources this way as well as avoided possible synchronisation issues... – fabian Oct 22 '19 at 19:57

0 Answers0