1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package examples
import kotlinx.coroutines.*
import kotlinx.coroutines.future.*
import kotlinx.coroutines.swing.*
import java.awt.*
import java.util.concurrent.*
import javax.swing.*
private fun createAndShowGUI() {
val frame = JFrame("Async UI example")
frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
val jProgressBar = JProgressBar(0, 100).apply {
value = 0
isStringPainted = true
}
val jTextArea = JTextArea(11, 10)
jTextArea.margin = Insets(5, 5, 5, 5)
jTextArea.isEditable = false
val panel = JPanel()
panel.add(jProgressBar)
panel.add(jTextArea)
frame.contentPane.add(panel)
frame.pack()
frame.isVisible = true
GlobalScope.launch(Dispatchers.Swing) {
for (i in 1..10) {
// 'append' method and consequent 'jProgressBar.setValue' are called
// within Swing event dispatch thread
jTextArea.append(
startLongAsyncOperation(i).await()
)
jProgressBar.value = i * 10
}
}
}
private fun startLongAsyncOperation(v: Int) =
CompletableFuture.supplyAsync {
Thread.sleep(1000)
"Message: $v\n"
}
fun main(args: Array<String>) {
SwingUtilities.invokeLater(::createAndShowGUI)
}
|