I'm not fully understanding binding. I have a cont...
# tornadofx
b
I'm not fully understanding binding. I have a controller with a couple of properties (of type Int and of type String) that I want to bind to labels on the view.
n
i think i can help when you share some of your code most likely zou make a copy of the value at some point and the observable then does nto actually get updates the class to use would be SImpleStringProperty for Strings
b
Here's an abbreviated bit of the relevant code:
Copy code
class Combatant(name: String) {
    val experienceProperty = SimpleIntegerProperty(100)
    var experience by experienceProperty
}

class EncounterBuilderController : Controller() {
    val availableCombatants = SimpleListProperty<Combatant>(observableList(direBadger, goblin))
    val selectedCombatants = SimpleListProperty<Combatant>(observableList())

    val experiencePoints: Int
        get() = selectedCombatants.map { it.experience }.sum()

    val challengeRating: String
        get() = challengeRatingFor(experiencePoints)

    fun addCombatant(combatant: Combatant?) {
        if (combatant != null) {
            selectedCombatants += combatant
        }
    }
}

class EncounterBuilderView : View("Encounter Builder") {
    val controller: EncounterBuilderController by inject()
    override val root = vbox {
        label("Encounter XP:")
        label(stringBinding(controller.experiencePoints) {
            "%,d".format(controller.experiencePoints)
        })
        label("CR:")
        label(controller.challengeRating)
    }
}
I think that the key thing here is that I don't understand how to wrap a dynamic Kotlin property into an observable or a JavaFX property.
n
challengeRating depends on a int val, that will never update
you want a
val experiencePointsProperty = integerBinding(selectedCombatantsProperty) {...}
and
val experiencePoints by experiencePointsProperty
selectedCombatantsProperty has to be a ListProperty too
and then the label will be created like this:
label(controller.challengeRatingProperty)
same for experiencePoints
b
Thank you! I'll definitely give that a try.
Awesome! That did it! Thank you again.