title: Duplicate series added Javafx
body: <p>I try to build a simple JavaFX application using <code>LineChart</code>. I want to add save all the series that I added in the graph, click after click.</p>
  
  <p>I saw other responses about this problem, but even if i cleaned all the Lists of my Service class, there are duplicated Series displayed.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>Application</strong></p>
  
  <pre><code>public class App extends Application {
  
  @Override
  public void start(Stage stage) throws Exception {
      final NumberAxis xAxis = new NumberAxis();
      final NumberAxis yAxis = new NumberAxis();
      BorderPane b = new BorderPane();
      MyService ser  =new MyService();
      LineChart&lt;Number, Number&gt; chart = new LineChart&lt;&gt;(xAxis, yAxis);
  
      Button button = new Button("start");
  
      Button button1 = new Button("start2");
      button.setOnAction(((event)-&gt;{
          ser.addEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, //
                  new EventHandler&lt;WorkerStateEvent&gt;() {
  
                      @Override
                      public void handle(WorkerStateEvent t) {
  
                          chart.getData().add(ser.getValue());
  
                      }
                  });
  
  
          ser.restart();
  
      }));
  
      button1.setOnAction(((event) -&gt;{
          XYChart.Series&lt;Number, Number&gt; series = new XYChart.Series&lt;Number, Number&gt;();
          series.getData().add(new XYChart.Data&lt;Number, Number&gt;(Math.random(), Math.random()));
          series.getData().add(new XYChart.Data&lt;Number, Number&gt;(Math.random(), Math.random()));
          series.getData().add(new XYChart.Data&lt;Number, Number&gt;(Math.random(), Math.random()));
          chart.getData().add(series);
      }));
  
  
  
      b.setBottom(button);
      b.setCenter(chart);
      b.setLeft(button1);
  
      Scene sc = new Scene(b);
  
  
      stage.setScene(sc);
      stage.show();
  
  }
  
  
  
  public static void main(String[] args) {
      launch(args);
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>My service class</strong></p>
  
  <pre><code>public class MyService extends Service&lt;XYChart.Series&lt;Number, Number&gt;&gt; {
      @Override
      protected Task&lt;XYChart.Series&lt;Number, Number&gt;&gt; createTask() {
          return new Task&lt;XYChart.Series&lt;Number, Number&gt;&gt;() {
              @Override
              protected XYChart.Series&lt;Number, Number&gt; call() throws Exception {
                  XYChart.Series&lt;Number, Number&gt; series = new XYChart.Series&lt;Number, Number&gt;();
  
  
                  series.getData().add(new XYChart.Data&lt;Number, Number&gt;(Math.random(), Math.random()));
              series.getData().add(new XYChart.Data&lt;Number, Number&gt;(Math.random(), Math.random()));
              series.getData().add(new XYChart.Data&lt;Number, Number&gt;(Math.random(), Math.random()));
  
  
                  return series;
              }
          };
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/qEBD0.png" rel="nofollow noreferrer">We can see duplicated series with the labels</a></p>
  
  <p>maybe I have to go through a temporary variable then add the series outside the handle.
  In this case it is good except that I have a null value of seriesTotal..</p>
  
  <pre><code>public class App extends Application {
  
      final NumberAxis xAxis = new NumberAxis();
      final NumberAxis yAxis = new NumberAxis();
  
      private XYChart.Series&lt;Number, Number&gt; seriesTotal = new XYChart.Series&lt;Number, Number&gt;();
  
      private LineChart&lt;Number, Number&gt; chart = new LineChart&lt;&gt;(xAxis, yAxis);
  
      @Override
      public void start(Stage stage) throws Exception {
  
          BorderPane b = new BorderPane();
          MyService ser = new MyService();
  
          // Job j = new Job();
          Button button = new Button("start");
          Button button1 = new Button("start2");
  
          List&lt;Object&gt; test = new ArrayList&lt;&gt;();
  
          button.setOnAction(((event) -&gt; {
              test.clear();
              ser.addEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, //
                      new EventHandler&lt;WorkerStateEvent&gt;() {
  
                          @Override
                          public void handle(WorkerStateEvent t) {
  
  
                              seriesTotal = ser.getValue();
                              //test.add(ser.getValue());
                              //chart.getData().add(ser.getValue());
  
  
                          }
  
                      });
  
  
  
              ser.restart();
  
              System.out.println(seriesTotal);
              chart.getData().add(seriesTotal);
              seriesTotal.getData().clear();
          }));
  
  
  
          button1.setOnAction(((event) -&gt; {
              XYChart.Series&lt;Number, Number&gt; series1 = new XYChart.Series&lt;Number, Number&gt;();
              series1.getData().add(new XYChart.Data&lt;Number, Number&gt;(Math.random(), Math.random()));
              series1.getData().add(new XYChart.Data&lt;Number, Number&gt;(Math.random(), Math.random()));
              series1.getData().add(new XYChart.Data&lt;Number, Number&gt;(Math.random(), Math.random()));
              chart.getData().add(series1);
              // chart.getData().setAll(arg0);
          }));
  
          b.setBottom(button);
          b.setCenter(chart);
          b.setLeft(button1);
  
          Scene sc = new Scene(b);
  
          stage.setScene(sc);
          stage.show();
  
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  
  }
  enter code here
  </code></pre>