JavaFX可以处理循环属性绑定图吗?

jhe*_*dus 2 binding javafx javafx-2 javafx-8

让我们考虑三个JavaFX属性:AB C.

现在让我们将它们双向绑定到三角形(AB,BC,AC).

现在我们假设我们修改了A的值.

这是否会导致问题(例如无限递归)?

JavaFX可以处理这种循环绑定图吗?如果是的话,它是如何做到的?

谢谢阅读.

Jam*_*s_D 6

试试吧...

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;


public class CyclicPropertyTest {

    public static void main(String[] args) {
        IntegerProperty x = new SimpleIntegerProperty(0);
        IntegerProperty y = new SimpleIntegerProperty(1);
        IntegerProperty z = new SimpleIntegerProperty(2);

        x.addListener((obs, oldValue, newValue) -> System.out.printf("x changed from %d to %d %n", oldValue, newValue));
        y.addListener((obs, oldValue, newValue) -> System.out.printf("y changed from %d to %d %n", oldValue, newValue));
        z.addListener((obs, oldValue, newValue) -> System.out.printf("z changed from %d to %d %n", oldValue, newValue));

        x.bindBidirectional(y);
        y.bindBidirectional(z);
        z.bindBidirectional(x);

        x.set(1);
    }

}
Run Code Online (Sandbox Code Playgroud)

仅在属性值更改时才通知侦听器.当x设置为1时,这会导致y设置为1,这会导致z设置为1.这会激活侦听器.由于z已经改变,这导致(至少在概念上)x被设置为1,但由于它已经是1,所以没有通知监听器,因此循环终止.