Javafx 依赖元素的布尔绑定

Javafx 依赖元素的布尔绑定,javafx,binding,Javafx,Binding,我想这样创建布尔绑定: BooleanBinding binding = Bindings.createBooleanBinding( () -> (context.getService() == null || context.getService().getCurrentFamily() == null), context.serviceProperty(), context.getServ

我想这样创建布尔绑定:

    BooleanBinding binding = Bindings.createBooleanBinding(
            () -> (context.getService() == null
                    || context.getService().getCurrentFamily() == null),
            context.serviceProperty(), context.getService().familyProperty());
但在初始化时,serviceProperty的值为null,因此我在context.getService.familyProperty上收到NullPointerException


如何初始化此绑定。正确吗?

使用标准API,您可以

BooleanBinding binding = Bindings.select(context.serviceProperty(), family).isNull();
如果本例中的服务为null,则会在标准输出上生成警告,这是一个令人讨厌的习惯,尽管存在这些警告,但它仍然可以正常工作。对于这样的用例,我喜欢Tomas Mikula或库。例如,使用ReactFX 2.0,您可以按照

ObservableValue<Boolean> binding = 
    Val.map(context.serviceProperty(), service -> service.familyProperty())
        .flatMap(family -> family.isNull());

您有一个更基本的问题:绑定将只绑定到当前服务的familyProperty。如果服务发生了可能发生的更改,因为您绑定到context.serviceProperty,那么您仍将绑定到观察旧服务的familyProperty上的更改,而不是新服务的familyProperty上的更改。因此,即使您解决了NPE的问题,在服务更改之后,对服务系列的更改也不会更新binding.jeah。。你说得对。。所以我必须初始化两个听众来听。服务变化和家庭内部变化。。。谢谢