mineshmshah
mineshmshah

Reputation: 480

JavaFX Listener to check for a boolean value

I have a game with a GUI with a couple of buttons that you press to eventually complete the game.

I have the following boolean to test:

public boolean complete(){
    if (initvalue==finalvaue)){
        return true;
    }
    else {
        return false;
    }
}

I want to create a listener to check if the game is finished and to print out a message to the user. I think I need to create a SimpleBooleanProperty for this to work. But would I need to bind it to the boolean value above?

What would the best way to go about this?

Upvotes: 5

Views: 15747

Answers (1)

DVarga
DVarga

Reputation: 21829

You can have a BooleanProperty called completedProperty and you can have initvalueProperty and finalValueProperty also to store the points of the game, and then you can simply bind the value of the completedProperty like

completedProperty.bind(initValueProperty.isEqualTo(finalValueProperty));

therefore you don't even need a method anymore.

In the example I have just placed a Button which increments the current value by one on press and I used 3 properties to continuously check the "game" state. As soon as the completedProperty switches to "true", a Label with text of "Game Over" appears.

Note: I have used IntegerProperty to store the current and the final value, but you can use any Property.

Example:

public class Main extends Application {

    private IntegerProperty initValueProperty = new SimpleIntegerProperty(0);
    private IntegerProperty finalValueProperty = new SimpleIntegerProperty(10);
    private BooleanProperty completedProperty = new SimpleBooleanProperty();

    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);

            Button buttonPlusOne = new Button("+1");
            root.setCenter(buttonPlusOne);

            Text currValueText = new Text();
            currValueText.textProperty().bind(initValueProperty.asString());
            root.setBottom(currValueText);

            // Increment current value
            buttonPlusOne.setOnAction(e -> initValueProperty.set(initValueProperty.get()+1));
            // Bind completed property: initValue equals finalValue
            completedProperty.bind(initValueProperty.isEqualTo(finalValueProperty));

            completedProperty.addListener((observable, oldValue, newValue) -> {
                // Only if completed
                if (newValue) 
                    root.setTop(new Label("Game Over"));
            });

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

Optionally (as noted by fabian) you can even use a BooleanBinding rather than a property to check the completed state (in this case you don't need completedProperty), like:

BooleanBinding completed = initValueProperty.isEqualTo(finalValueProperty);
completed.addListener((observable, oldValue, newValue) -> {
    // Only if completed
    if (newValue) 
        root.setTop(new Label("Game Over"));
});

To learn about properties in JavaFX this is a really good article to start with.

Upvotes: 6

Related Questions