java - Where to add events to nodes? -
can tell me, should declare event listeners nodes, added out side of controller class?
the best way explain example:
i have controller:
public class fxmldocumentcontroller implements initializable { @fxml private anchorpane root; @override public void initialize(url url, resourcebundle rb) { testtask test = new testtask(root); thread th = new thread(test); th.start(); } }
and have task, started in initialize method:
public class testtask extends task<void>{ private anchorpane root; public testtask(anchorpane root){ this.root = root; } @override protected void call() throws exception { button btn = new button("testbutton"); platform.runlater(() -> { root.getchildren().add(btn); }); return null; } }
what i'm doing here? have fxml anchorpane root element. has id root. start task in add 1 button root node. want register action event button. question now, can/should register listener. register them in controller, here can't because button exists in task class. register in task class think not scales large applications. other way return node back, can access in controller class, here have check if added (to have call task.get()
, stops application. tell me: best way register listener node?
don't create ui in background thread. there (at best) need this. if need perform long-running task retrieves data need in order create ui, return data task, , create ui in task's onsucceeded
handler:
public class somecontrollerclass { @fxml private anchorpane root ; public void initialize() { task<somedatatype> task = new mytask(); task.setonsucceeded(e -> { // method executed on fx application thread. somedatatype result = task.getvalue(); // create ui , update root, using data retrieved }); thread thread = new thread(task); thread.start(); } }
and
public class mytask extends task<somedatatype> { @override public somedatatype call() { somedatatype result = longrunningprocess(); return result ; } }
Comments
Post a Comment