1 // This file was automatically generated from coroutines-guide-ui.md by Knit tool. Do not edit. 2 package kotlinx.coroutines.javafx.guide.exampleUiBasic03 3 4 import kotlinx.coroutines.* 5 import kotlinx.coroutines.channels.* 6 import kotlinx.coroutines.javafx.JavaFx as Main 7 import javafx.application.Application 8 import javafx.event.EventHandler 9 import javafx.geometry.* 10 import javafx.scene.* 11 import javafx.scene.input.MouseEvent 12 import javafx.scene.layout.StackPane 13 import javafx.scene.paint.Color 14 import javafx.scene.shape.Circle 15 import javafx.scene.text.Text 16 import javafx.stage.Stage 17 mainnull18fun main(args: Array<String>) { 19 Application.launch(ExampleApp::class.java, *args) 20 } 21 22 class ExampleApp : Application() { <lambda>null23 val hello = Text("Hello World!").apply { 24 fill = Color.valueOf("#C0C0C0") 25 } 26 27 val fab = Circle(20.0, Color.valueOf("#FF4081")) 28 <lambda>null29 val root = StackPane().apply { 30 children += hello 31 children += fab 32 StackPane.setAlignment(hello, Pos.CENTER) 33 StackPane.setAlignment(fab, Pos.BOTTOM_RIGHT) 34 StackPane.setMargin(fab, Insets(15.0)) 35 } 36 <lambda>null37 val scene = Scene(root, 240.0, 380.0).apply { 38 fill = Color.valueOf("#303030") 39 } 40 startnull41 override fun start(stage: Stage) { 42 stage.title = "Example" 43 stage.scene = scene 44 stage.show() 45 setup(hello, fab) 46 } 47 } 48 setupnull49fun setup(hello: Text, fab: Circle) { 50 val job = GlobalScope.launch(Dispatchers.Main) { // launch coroutine in the main thread 51 for (i in 10 downTo 1) { // countdown from 10 to 1 52 hello.text = "Countdown $i ..." // update text 53 delay(500) // wait half a second 54 } 55 hello.text = "Done!" 56 } 57 fab.onMouseClicked = EventHandler { job.cancel() } // cancel coroutine on click 58 } 59