I have problem, when I want add Node to my GUI from other Thread. It throws IllegalStateException and I don’t know how to fix it.
public class DashBoardController implements Initializable {
@FXML
private FlowPane dashBoardPane;
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
RTMClientV2 client = new RTMClientV2("localhost", 9009, new DashBoardArranger(this));
Thread clientTH = new Thread(client);
clientTH.start();
} catch (IOException ex) {
Logger.getLogger(DashBoardController.class.getName()).log(Level.SEVERE, null, ex);
}
}
public synchronized void addToDashBoard(Pane root){
dashBoardPane.getChildren().add(root);
}
}
I just load my .FXML file to GUI with this controller and when program starts it runs Thread responsible for communication with server (clientTH.start();) and everything is OK. But when server send data after init. and I want this data add to my Dashboard, I use method public synchronized void addToDashBoard(Pane root) as before, but it throws java.lang.IllegalStateException and I have no idea why.
btw: I have found this: “If this Parent node is attached to a Scene, then its list of children must only be modified on the JavaFX Application Thread. An IllegalStateException is thrown if this restriction is violated.”, but it is not useful for me. Dashboard is added to another Pane in my GUI.
btw: Output:
Exception in thread “Thread-4” java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source)
at javafx.scene.Parent$1.onProposedChange(Unknown Source)
at com.sun.javafx.collections.VetoableObservableList.add(Unknown Source)
at com.sun.javafx.collections.ObservableListWrapper.add(Unknown Source)
at probeobserver.gui.probeSite.DashBoardController.addToDashBoard(DashBoardController.java:125)
at probeobserver.gui.probeSite.DashBoardArranger.setCompName(DashBoardArranger.java:66)
at probeobserver.rtm.RTMClientV2.readAllDataAndUpdate(RTMClientV2.java:144)
at probeobserver.rtm.RTMClientV2.run(RTMClientV2.java:80)
at java.lang.Thread.run(Thread.java:722)
In your I/O thread, you need to interact with the UI within the UI thread: