Bidirectional inverted binding between two BooleanProperties

Viewed 140

First, naive attempt:

booleanProperty1.bindBidirectional(booleanProperty2.not()); // type mismatch

This doesn't work. I either have to change it to unidirectional bind (so it will accept BooleanBinding as an argument) or remove .not().

What would be the best way to actually achieve my goal here?

2 Answers

The most easiest way is to include listeners to both the properties and inverse the value to set on other property.

booleanProperty1.addListener((obs,old,val)->booleanProperty2.set(!val));
booleanProperty2.addListener((obs,old,val)->booleanProperty1.set(!val));

Add a listener to booleanProperty2.

Upon its value changing (triggering the listener), set booleanProperty to the opposite of booleanProperty2.

Related