This discussion is archived
2 Replies Latest reply: Oct 31, 2013 7:42 AM by user11175597 RSS

remove children HBox (Multithread)

user11175597 Newbie
Currently Being Moderated

Hello,

 

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 :

[CODE]

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) {

            log.error(e);

       }

 

       //reinit hbox

       HBox myHbox = new HBox();

 

        //add nodes to hbox

        addInfosToHbox(myHbox);

 

 

        return myHbox;

    }

};

 

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

Thread th = new Thread(taskProcessWord);

th.setDaemon(true);

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

th.start();

[/CODE]

 

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

  • 1. Re: remove children HBox (Multithread)
    James_D Guru
    Currently Being Moderated

    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>() {
         @Override
         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

    hboxResultat.getChildren().setAll(taskProcessWork.getValue());

    in your onSucceeded handler.

  • 2. Re: remove children HBox (Multithread)
    user11175597 Newbie
    Currently Being Moderated

    Thank you !

     

    It works perfectly

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points