Skip to content
Advertisement

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

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?

Advertisement

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement