@@ -4,10 +4,9 @@ import javafx.scene.Scene
44import javafx.scene.control.Label
55import javafx.scene.layout.Pane
66import javafx.stage.Stage
7- import kotlinx.coroutines.Dispatchers
8- import kotlinx.coroutines.GlobalScope
9- import kotlinx.coroutines.delay
10- import kotlinx.coroutines.launch
7+ import kotlinx.coroutines.*
8+ import kotlinx.coroutines.javafx.JavaFx
9+ import kotlin.coroutines.CoroutineContext
1110
1211class MillisElapsedCounter (private val observer : Observer ) {
1312 fun start () {
@@ -27,7 +26,10 @@ class MillisElapsedCounter(private val observer: Observer) {
2726 }
2827}
2928
30- class MyApp : Application (), MillisElapsedCounter.Observer {
29+ class MyApp : Application (), MillisElapsedCounter.Observer, CoroutineScope {
30+ override val coroutineContext: CoroutineContext
31+ get() = Dispatchers .JavaFx
32+
3133 private val button = Label ().also { it.text = " waiting for 0s" }
3234
3335 override fun start (primaryStage : Stage ? ) {
@@ -42,7 +44,9 @@ class MyApp : Application(), MillisElapsedCounter.Observer {
4244 }
4345
4446 override fun notify (millisElapsed : Int ) {
45- button.text = " waiting for ${millisElapsed / 1000 } s"
47+ launch {
48+ button.text = " waiting for ${millisElapsed / 1000 } s"
49+ }
4650 }
4751}
4852
0 commit comments