JavaFX binding and null values

后端 未结 2 1578
暖寄归人
暖寄归人 2021-02-14 06:38

I was wondering how to bind values where the source of the bind could be null.

I have a property:

private ObjectProperty operation = new         


        
2条回答
  •  迷失自我
    2021-02-14 07:07

    If a 3rd party library is an option, check out EasyBind. Try something like this:

    EasyBind.select(operation)
            .selectObject(Operation::nameProperty)
            .orElse("null");
    

    There's also a JavaFX JIRA issue for the type of functionality provided by EasyBind. If you don't want to use a 3rd party library, try Bindings.select:

    Bindings.when(operation.isNotNull())
        .then("null")
        .otherwise(Bindings.select(operation, "name"));
    

    Be aware the null checking in Bindings.select isn't super efficient. There's a JIRA issue for it.

提交回复
热议问题