Adding a listener to a variable in Java/JavaFX which gets called on variable change

Tags: , ,



I know that there are listeners in JavaFX, and i’m sure Java. But I’m confused as how to implement them.

I have a boolean variable that is changed throughout my program. Everytime the boolean is changed, I want a function myFunc() to be ran.

Can this be done easily?

Answer

As simple as this:

public void changeBooleanFlag(boolean bEnabled)
{
    if(booleanFlag == bEnabled) return;
    booleanFlag = bEnabled;
    myFunc();
}

and whenever you want to change the boolean flag, you should do it via this method.



Source: stackoverflow