Description
I'm trying to get a view to refresh each time it is docked but I'm running into trouble. To illustrate what's happening, I've set up a minimal code sample based off the IDEA template for a new tornadofx project with maven. I've edited the generated MainView to look as follows:
package com.example.demo.view
import tornadofx.*
class MainView : View("Hello TornadoFX") {
private val wurds = observableList<String>()
override val root = hbox(10)
init {
wurds.onChange {
redrawContent()
}
}
private fun redrawContent() {
root.replaceChildren {
wurds.forEach {
label(it)
}
}
}
override fun onDock() {
println("ondock")
onRefresh()
}
override fun onRefresh() {
println("onrefresh")
with(root) {
runAsyncWithOverlay {
Thread.sleep(1000)
listOf("Hello", "Edvin")
} ui {
wurds.setAll(it)
}
}
}
}
My idea was to use an observable list as a sort of model of what labels should be shown and registering a listener to changes in the model from init
. When changes happen, and each time the view is docked, I want the model to be refreshed based on the results of a database query (here replaced by sleeping the thread). The code above does not execute but instead gets stuck in an infinite loop of docking and calling refresh.
I believe the underlying reason must be that runAsyncWithOverlay actually undocks and redocks the view but I'm not sure.
How would I ideally achieve what I'm trying to do?