Search code examples
kotlintornadofx

Handling nested Properties in JavaFX / TornadoFX


I want to have a window which shows info about certain ViewModel

Suppose you have a simple Person:

class Person(name: String) {
    val nameProperty = SimpleStringProperty(name)
}

and have instance of Person save in property:

val personProperty = SimpleObjectProperty(Person("John"))

what's the correct solution to show Person's name in label?

Using this:

label(personProperty.value.nameProperty)

Will not update when I update the property's person:

personProperty.value = Person("Joe")

(That's obvious because only the reference changes, not the value itself)

So is there any good way to do this or do I have to manually add listeners for personProperty and update which Person does label point to?

EDIT: I also found this question: JavaFX binding and property change, but it doesn't contain anything new and useful that I didn't know about, is there any TornadoFX-specific way of doing this?


Solution

  • This is exactly what the ItemViewModel does for you. If you want to make a binding for the name property that updates automatically, outside of an ItemViewModel, you can use the TornadoFX feature select:

    val nameProperty = personProperty.select { it.nameProperty }