Search code examples
kotlinjavafxtornadofx

How to correctly update value of IntegerProperty in the view?


I am building simple application that will track a certain tabletop game of 2 players.

I have a view called MatchView

class MatchView : View() {

    // data
    private var currentRound = SimpleIntegerProperty(0)
    private var currentTurn = SimpleIntegerProperty(0)

    override val root = borderpane {
        center = label(currentRound.stringBinding{ "Round %d".format(it) })
        // other layout-related stuff
        subscribe<EndTurnEvent> {
            endPlayerTurn()
        }
    }

    private fun endPlayerTurn() {
        // increment turn
        currentTurn.plus(1)
        currentRound.plus(currentTurn.value % 2)
    }
}

that is subscribed to EndTurnEvent - event emitted by one of the fragments used by view. The called method is supposed to increment value of currentTurn and if needed currentRound (round increments after second player ends their turn)

However neither the value of currentRound nor the one of currentTurn are getting increased when i call .plus() method.

I have tried editting values differently :

private fun endPlayerTurn() {
    // increment turn
    currentTurn.value = currentTurn.value + 1
    currentRound.value = currentTurn.value % 2
}

But this throws me java.lang.IllegalStateException: Not on FX application thread

I am aware that putting properties into views is anti-pattern, but since I just want to keep track of 2 properties, I thought I could put them directly into View


Solution

  • Platform.runLater(() -> {
            // Update GUI from another Thread here
        });