简体   繁体   中英

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.

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] }) {
    ...
}

I dont really understand why it fixed it, but I tried to put the value in a SimpleStringProperty and pass that variable to the imageview, and now its updating on buttonclick.

val simplestring = SimpleStringProperty("slime.png")
...
imageview(observablepic)
center = button("Change image") {
    action { 
        currentenemy.value += 1 
        simplestring.value = controller.monsters[currentenemy.value]
    }
}
...
class MyController: Controller() {
    val monsters = FXCollections.observableArrayList("slime.png", "goblin.png", "mage.png", "knight.png", "boss.png")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM