I have a label in my fxml file:
<Label fx:id="labelA"/>
I want to update this in the controller during a background worker is executing. I have tried to do something like this:
public class FXHelloCVController
{
@FXML
private Label labelA;
@FXML
protected void startMatching(ActionEvent event)
{
SwingWorker<Boolean, String> worker = new SwingWorker<Boolean, String>()
{
@Override
protected Boolean doInBackground() throws Exception
{
for(int y=0; y<*something*; y++){
if(n.get(y)!=null){
...
publish(n.get(y).name);
...
}
}
return true;
}
@Override
protected void process(List<String> chunks) {
String n = chunks.get(chunks.size()-1);
labelA.setText(n);
}
};
worker.execute();
return;
}
But when the function "labelA.setText(n)" is called, it appears the following exception:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0
What is wrong?