2 Replies Latest reply on Oct 31, 2013 2:42 PM by user11175597

    remove children HBox (Multithread)




      I'm trying to use the Task and Service classes to do multithreading treatment on my javafx application. But im quite confused about it.


      Here are the steps i want to follow :

         1) I click on one of my IHM button.

         2) the click handler launch a parallel thread (Task) which execute complex and long treatment. In the meantime the javafx thread is terminating and the gui is released.

         3) When the calculation are finished on the parallel thread, it change the children on one of my ihm hbox.



      Here the code i made.


      click_handler :


      HBox hboxResultat = (HBox) vboxListInput .getChildren().get(indexHbox);


      Task<HBox> taskProcessWord = new Task<HBox>() {

          @Override protected HBox call() throws Exception {

             try {

                  //Long and complex calculation.

             } catch (Exception e) {




             //reinit hbox

             HBox myHbox = new HBox();


              //add nodes to hbox




              return myHbox;




      hboxResultat = taskProcessWord.valueProperty().get();

      Thread th = new Thread(taskProcessWord);


      System.out.println("Starting background task...");




      the code above doesn't produce any result. Can u help me?

        • 1. Re: remove children HBox (Multithread)

          Two issues:


          1. When it completes, your Task sets its value to a new HBox instance, populated with nodes. Simply assigning this to the same variable you used to add a different HBox into your scene graph won't update the scene graph; you need to actually do that by (for example) removing the old HBox and putting the new one in its place.


          2. You are calling taskProcessWord.valueProperty.get() before the Task completes (in fact, before it even starts). At this point the value will simply be null. You need to call this method once the task has completed: use Task.setOnSucceeded(...) to do this.


          So you want something like


          taskProcessWord.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
               public void handle(WorkerStateEvent event) {
                    vboxListInput.getChildren().set(indexHbox, taskProcessWord.getValue());


          after you create the task but before you start it.

          You will need to make taskProcessWord and indexHbox final for this to compile.


          You could also consider having your task return a List<Node> instead, and call


          in your onSucceeded handler.

          • 2. Re: remove children HBox (Multithread)

            Thank you !


            It works perfectly