Search code examples
javaandroidkotlindesktop-applicationtornadofx

Kotlin refresh imageview TornadoFX


Im trying to refresh the image of an imageview on a button clink, but it doesnt seems like its working. Here is my code:

class MasterView : View("Master View") {
    val controller: MyController by inject()
    val currentenemy = SimpleIntegerProperty(0)

    override val root = borderpane() {
        right = imageview(controller.monsters[currentenemy.value]) {
            setFitHeight(250.0)
            setFitWidth(175.0)
        }
        center = button("Change image") {
            action { currentenemy.value += 1 }
        }
    }
}
class MyController: Controller() {
    val monsters = FXCollections.observableArrayList("slime.png", "goblin.png", "mage.png", "knight.png", "boss.png")
}

I know that the values are changeing, but the imageview doesn't seem to care about it.


Solution

  • You are creating the image view with a string value. There's nothing there to tell the image view to update when your currentEnemy property changes. For this, you need to give the image view an observable value that it can track.

    For example:

    right = imageview(currentEnemy.stringBinding { controller.monsters[it ?: 0] }) {
        ...
    }