Reputation: 3
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?
Upvotes: 0
Views: 256
Reputation: 390
Platform.runLater: If you need to update a GUI component from a non-GUI thread, you can use that to put your update in a queue and it will be handle by the GUI thread as soon as possible.
Platform.runLater(new Runnable() {
@Override public void run() {
// update an FXML node (e.g. label)
}
});
Source: Platform.runLater and Task in JavaFX
But you're using Swing and JavaFX, which isn't usually.
Upvotes: 2
Reputation: 10650
A SwingWorker is for Swing and not for JavaFX. Have a look at the documentation for the corresponding JavaFX concurrency mechanisms.
Upvotes: 1