JavaFX Alert Information not showing ContentText anymore don't know why

Viewed 311

enter image description hereI have a small application in JavaFX running on Windows 10, I've changed many things inside the application and now I am facing the problem that my InfoBox is showing up with header and Picture but no text is wiritten inside the box.

I am calling this inside a part of my app:

 infoBox("Karte wurde gelesen... bitte warten ", "Information-NFC", "Information-NFC!");

and here is the function itself:

public  void infoBox(String infoMessage, String titleBar, String headerMessage) 
{
   
      
    Alert alert = new Alert(AlertType.INFORMATION);
    alert.setResizable(true);
    alert.getDialogPane().setPrefSize(480, 320);

    alert.getDialogPane().setStyle("-fx-text-fill: red;-fx-font-size: 15px; -fx-font: 10px Tahoma; ");

    alert.setTitle(titleBar);
    alert.setHeaderText(headerMessage);
   alert.setContentText(infoMessage);



    System.out.println(infoMessage+"here");
        //Logo für Programm Fenster
//////////// Logo ProgrammFesnter end
Image img2 = new Image(getClass().getResourceAsStream("logo.PNG"));
Stage dialogStage = (Stage) alert.getDialogPane().getScene().getWindow();
dialogStage.getIcons().add(img2);
    alert.show();
   try {
       Thread.sleep(8000);
   } catch (InterruptedException ex) {
       System.out.println(ex);
   }
    alert.close();
}

I can see in the log window that the variable "infoMessage" is set but the Alert Box itself is empty. And I don't know why. Anyone of you out there had this problem before?

Everything is running fine except this small little box... ???

Thanks to all out there and have nice holidays....

Stev

1 Answers

Most likely the problem is the Thread.sleep(8000) call. That sleeps the JavaFX Application Thread which prevents user interaction with the UI and also prevents any render "pulses" from being scheduled. In other words, your application is unresponsive for those 8 seconds.

If you want to delay an action on the FX thread then you should use the animation API. And since you appear to want to wait for the alert to close before allowing the code to proceed you should also make use of the onShown property and showAndWait() method of Dialog. For example:

Alert alert = ...;
// configure alert

PauseTransition closeDelay = new PauseTransition(Duration.seconds(8));
closeDelay.setOnFinished(e -> alert.close());

alert.setOnShown(e -> closeDelay.playFromStart());
alert.showAndWait();
Related