Reputation: 11
I tried to implement a chat application using socket connection in Kotlin and TornadoFX library to make the GUI. The problem comes when I try to launch the client because it keeps waiting a message from the Server although I put that code that update the label and receive the message inside a runAsync. I red the TornadoFX documentation and saw youtube videos but I cannot come to the solution. I know that the issue is that the program is stuck in that block but can't figure how to do it.
class MyFirstView: View("Chat"){
var input: TextField by singleAssign()
var test = SimpleStringProperty()
val client: Client by inject()
init {
client.connect()
val t = thread(true) {
while (true) {
random = client.getMessage()
println(random)
Platform.runLater { test.set(random) }
}
}
}
override val root = vbox {
hbox {
label(test) {
bind(test)
}
}
hbox {
label("Write here some text")
input = textfield()
}
hbox {
button("Send") {
action{
client.writer.println(input.text)
}
}
}
}
}
Upvotes: 1
Views: 168
Reputation: 7297
You can only update UI elements on the UI thread, so if you want to manipulate the UI from a background thread, you need to wrap that particular code in runLater { }
.
On another note, you shouldn't manipulate the text of the textfield or store ui element references with singleAssign. Instead you should bind your textfield to a StringProperty and manipulate the value instead. This is covered in the guide, so check it out :)
Upvotes: 2