Bidirectionally bind to property negation

岁酱吖の 提交于 2019-12-12 06:07:41

问题


Given

DoubleProperty A;
DoubleProperty minusA;

is there a way to bind their negations bidirectionally to each other, so that A.get() == -minusA.get() at all times, and both can be set()?


回答1:


I tried but did´t find a way by using a bidirectional binding but maybe you could use an InvalidationListner on both?

Something like

A.addListener((Observable observable) -> {
        System.out.println("A is invalid");
        minusA.set(A.get() *-1);
    });

minusA.addListener((Observable observable) -> {
        System.out.println("minusA is invalid");
        A.set(minusA.get() * -1);
    });

then you could easily call the setter method of both DoubleProperties and the other value will change to the negativ value.

Hope that helps



来源:https://stackoverflow.com/questions/34439109/bidirectionally-bind-to-property-negation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!