JavaFX binding and null values

后端 未结 2 1577
暖寄归人
暖寄归人 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.

    0 讨论(0)
  • 2021-02-14 07:18

    Just in case if somebody using not Java itself but Kotlin. It is a good idea to use wonderful tornadofx library. There you can just use operation.select{it.name}. Although, this feature seems not to be documented yet, so it took some time to discover it.

    0 讨论(0)
提交回复
热议问题