1

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.

4
  • Why do you have to use showAndWait() to use a Button? Commented Oct 25, 2018 at 20:23
  • You could simply use Platform.runLater for this. show is available too, but you need to add some listener, if you want to get the result. Commented Oct 25, 2018 at 20:26
  • @Zephyr When using a alert in javafx, the only way ive seen to use a button use showAndWait as shown in the Alert class docs.oracle.com/javase/8/javafx/api/javafx/scene/control/… The most common way being Optional<ButtonType> result = alert.showAndWait(); Commented Oct 25, 2018 at 20:34
  • Why you don't add an event to your button directly ? Commented Oct 26, 2018 at 1:38

1 Answer 1

1

You can use the show() method, but you'll need to retrieve the result after the Alert is closed by setting an onCloseRequest() handler.

You can then determine which button was clicked with the alert.getResult() method.

Here's a simple program to demonstrate:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Button to show an alert
        Button btnShowAlert = new Button("Show Alert!");

        // Setup the button action
        btnShowAlert.setOnAction(event -> {

            // Create a simple Alert
            Alert alert = new Alert(Alert.AlertType.NONE);
            alert.setHeaderText(null);
            alert.setTitle("Just a title");
            alert.setContentText("A fun message");
//            alert.initOwner(owner); // Remove for this sample

            ButtonType buttonPlayAgain = new ButtonType("Play again");
            alert.getButtonTypes().setAll(buttonPlayAgain);
//            alert.setOnHidden(evt -> Platform.exit()); // Don't need this

            // Listen for the Alert to close and get the result
            alert.setOnCloseRequest(e -> {
                // Get the result
                ButtonType result = alert.getResult();
                if (result != null && result == buttonPlayAgain) {
                    System.out.println("Play Again!");
                } else {
                    System.out.println("Quit!");
                }
            });

            alert.show();

        });

        root.getChildren().add(btnShowAlert);

        // Show the Stage
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

I'm not using a button to show the alert. I'm using a button inside of the alert
That isn't the important button...that just shows the alert WITH buttons on it. I had to show the alert somehow in the example ...
Did you try running the example? ;)
The example you gave doesn't relate to what I'm trying to do. Look at my edit to the original post please.
An Alert is an Alert, no matter how you design it. The concept is the same.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.