I'm trying to create a game in javafx where when you win an alert pops up saying you won and there is the option to play again or to close the program. The problem is to use a button in an alert window you have to use alert.stopAndWait() which doesn't work with timelines.
Is there another way to control a button without this method or would there be a better way to code this?
Thank you in advanced.
Edit: Here's the code I have for my alert so far:
public static void alert(Alert.AlertType alertType, Window owner, String title, String message) {
Alert alert = new Alert(alertType);
alert.setHeaderText(null);
alert.setTitle(title);
alert.setContentText(message);
alert.initOwner(owner);
alert.show();
ButtonType buttonPlayAgain = new ButtonType("Play again");
alert.getButtonTypes().setAll(buttonPlayAgain);
alert.setOnHidden(evt -> Platform.exit());
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonPlayAgain){
// ... user chose Play Again"
System.out.println("play again");
} else
Platform.exit();
// if user clicks exit
The problem is that you cannot use showAndWait with a timeline. I'm trying to find an alternitive to using showAndWait.
showAndWait()to use aButton?Platform.runLaterfor this.showis available too, but you need to add some listener, if you want to get the result.