'Is there any way where a javafx button can listen from different event handlers?

Am new to javafx. I wanted to write a simple javafx program that when clicking a button for the first time, the color of a rectangle should change to something else, e.g green, and when clicking it for the second time, the color should change back to the original color. But the button is only allowing a single click only? Help if there is any solution on this.

Here is a code.



Solution 1:[1]

You can try using simple boolean value to check if color has been changed before:

    boolean flag = false;
    @FXML
    private void onButtonClick(){
        if (flag){
            //change to e.g. red
        } else {
            //change to e.g. green
        }
        flag = !flag;
    }

This code is simple and if you want only to change between two colors it will be enough :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xEdziu