java - How to use javaFX Task in my case - Not duplicate -


i know there answers questions did not understand problem in code.

why :

java.lang.illegalstateexception: not on fx application thread; currentthread = thread-4 

i'm trying add text task text flow using method in controller class, reason program fails on .getchildren() method .

call spliter in controller class:

btnsplit.setonmouseclicked(new eventhandler<event>() {             @override             public void handle(event event) {                 thread split = new thread(new spliter(custtosplit, controller.this));                 split.setdaemon(true);                 split.start();              }         }); 

class spliter constractor :

public spliter(file f, controller c){         this.c = c;         this.cust = f;      } c.updatehebflow("dir created: "+ newdir.getabsolutepath() , info_text); 

part of controller class :

@fxml     private textflow hebflow; @override     public void initialize(url location, resourcebundle resources) { assert hebflow != null : "fx:id=\"hebflow\" not injected: check fxml file 'mainxml.fxml'.";      public void updatehebflow(string text,string type){         normaltext = new text();         errortext = new text();         errortext.setfill(color.red);         infotext = new text();         infotext.setfill(color.blue);         switch(type){         case(error_text) :             errortext.settext(text);             hebflow.getchildren().addall(new text("/n"), errortext);             break;         case(info_text) :             infotext.settext(text);             hebflow.getchildren().addall(new text("/n"), infotext);             break;         case(normal_text) :             normaltext.settext(text);             hebflow.getchildren().addall(new text("/n"), normaltext);             break;         }     }    } 

call updatehebflow in spliter class:

try{ c.updatehebflow("script tultul", info_text); }catch (exception e){             e.printstacktrace();         } 

from understand cant change ui other class other controller ive made method in controller class make changes , call in task class, why i'm getting exception? if wrong wright way ?

from understand cant change ui other class other controller

actually correct statement is: "you cannot change ui thread other javafx ui thread". so, solution use platform.runlater() splitter as:

// java 8 platform.runlater(() -> {     c.updatehebflow("script tultul", info_text); });  // java 7 platform.runlater(new runnable() {     public void run() {         c.updatehebflow("script tultul", info_text);     } }); 

platform.runlater() guaranteed run blocks in javafx ui thread , in call order.


Comments