0
votes

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.

2

2 Answers

1
votes

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

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")
}