I am trying to create an environment, in which a GUI is set up, and while the user is modifying its components through listeners, another thread is adding new elements to it
You can still use your standalone class.
The rule is that changes to the UI must happen on the FX Application Thread. You can cause that to happen by wrapping calls from other threads that change the UI in Runnable
s that you pass to Platform.runLater(...)
.
In your example code, d.addElement(i)
changes the UI, so you would do:
class Thread2 implements Runnable {
private DifferentThreadJavaFXMinimal d;
Thread2(DifferentThreadJavaFXMinimal dt){
d=dt;
}
@Override
public void run() {
for (int i=0;i<4;i++) {
final int value = i ;
Platform.runLater(() -> d.addElement(value));
// presumably in real life some kind of blocking code here....
}
System.out.println("Hahó");
}
}