I am working with JavaFX and I need to somehow update a UI element from a worker thread.
My worker thread is constantly listening, through a socket, for incoming connections. When it detects one, it calls a method updateListOfConnections();
What I want to do is updateListOfConnections is pretty simple:
this.deviceList.clear();
this.deviceList.addAll(this.contactManager.getContacts().keySet());
if(this.deviceList.size() > 0){
updateConnectButtonState(false);
} else {
updateConnectButtonState(true);
}
As you can see, I affect the state of the UI for a couple items here and when I attempt to call updateListOfConnections from my thread, I get an exception.
Some possible solutions that I came up with (which are really unideal) are:
I simply have the user manually refresh the deviceList by invoking updateListOfConnections with the press of a button.
Or, have a loop constantly running and when it gets to a certain interval, reset the interval and invoke updateListOfConnections.
So my main question is how do I update an UI element from a worker thread in Java with JavaFX?