title: Multithreading and keeping a stable communication with GUI with JavaFx
body: <p>I'm trying to create a method that reads words from a file and display them on a JavaFx label . I'm doing this by using a Service and Task for the background work , while using the Platform.runLater() to communicate with the GUI . The problem is that the work is actually done , but it shows only the last word coming from the file . Here is the code : </p>
  
  <pre><code>@FXML
  private void launchLecture() {
       if (stateFile)
       mainApp.textTreatment();
      System.out.println("Done");
      if ((new File(mainApp.getLinkToPdf()).exists())) {
          File fl = new File(mainApp.getLinkToPdf());
  
          Service&lt;Void&gt; service = new Service&lt;Void&gt;() {
  
              @Override
              protected Task&lt;Void&gt; createTask() {
  
                  return new Task&lt;Void&gt;() {
  
                      @Override
                      protected Void call() throws Exception {
                          Scanner sc = new Scanner(fl);
                          final CountDownLatch latch = new CountDownLatch(1);
                          Platform.runLater(() -&gt; {
                              try {
                                  int count = 3;
                                  while (count &gt;= 0) {
                                      wordToShow.setText(count + "...");
                                      try {
                                          Thread.sleep(1000);
                                      } catch (InterruptedException e) {
                                          e.printStackTrace();
                                      }
                                      count--;
                                  }
                                  // ---
                                  while (sc.hasNext()) {
                                               wordToShow.setText(sc.next().toString());
                                      try {
                                          Thread.sleep(speed);
                                      } catch (InterruptedException e) {
                                          e.printStackTrace();
                                      }
                                  }
                              } catch (Exception e) {
                                  e.printStackTrace();
                              } finally{
                                  latch.countDown();
                              }
  
                          });
                          latch.await();
                          return null;
                      }
                  };
              }
          };
          service.start();
  
      }
  
  }
  </code></pre>
  
  <p>I tried many of the solutions I found on StackOverFlow but still can't get the problem fixed .</p>