1

I'm trying to make a simple loading window in JavaFX and I'm using an information alert for this. Here's my code:

public Alert alert = new Alert(Alert.AlertType.INFORMATION);

public void drawData(javafx.event.ActionEvent actionEvent) {
    alert.setHeaderText(null);
    alert.setTitle("Loading...");
    alert.show();
    for (int i = 0; i < * something * ; i++) {
        /* some code here */
        System.out.println(i);
        alert.setContentText(Integer.toString(i));
    }
    alert.close();
}
}

But this doesn't seem to work. All I'm getting is empty alert window: enter image description here

Is there really a way I can fix that? Also, how do I ignore the alert's OK button?

Arpan Sarkar
  • 2,301
  • 2
  • 13
  • 24
J. Doe
  • 75
  • 1
  • 1
  • 10
  • 2
    Possible duplicate of [JavaFX refresh Alert](https://stackoverflow.com/questions/48645732/javafx-refresh-alert) – SedJ601 Mar 29 '18 at 13:33

1 Answers1

0

The problem is that you do your work on the User interface thread:

for (int i =0; i < *something*; i++){
        *some code here*
        System.out.println(i);
        alert.setContentText(Integer.toString(i));
}

This blocks the user interface and any updates. In JavaFX updates are done on each 'tick' of the user interface scheduler. If you update properties of user interface items they will not get processed until the UI thread gets the next tick.

For work to be done you need to move it outside of the user interface thread and use a mechanism to update it. Check out JavaFX concurrency for more informtion on Worker and other concurrency classes which will take some trouble out of your hands.

M. le Rutte
  • 3,525
  • 3
  • 18
  • 31