Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8088998

LineChart: duplicate child added exception when remove & add a series

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 7u9, 7u21
    • Fix Version/s: tbd
    • Component/s: javafx
    • Environment:

      Windows 7, JRE 1.7.0_21

      Description

      When I remove a series from a LineChart and then add the same series back to the LineChart, I get an IllegalArgumentException because "duplicate children added." The error occurs even if I create a new XYChart.Series object and copy the data collection from the old series to the new series. The only way to avoid the error is to create a new XYChart.Series object and copy the data by creating new XYChart.Data instances.

      I first hit this problem using JRE 1.7.0_09 with JavaFX 2.2.3. I upgraded to the latest Java release (1.7.0_21 with JavaFX 2.2.21) and I see the same problem.

      The code below demonstrates the problem.

      package interactivechart1;

      import javafx.application.Application;
      import javafx.collections.ObservableList;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Node;
      import javafx.scene.Scene;
      import javafx.scene.chart.LineChart;
      import javafx.scene.chart.NumberAxis;
      import javafx.scene.chart.XYChart;
      import javafx.scene.chart.XYChart.Series;
      import javafx.scene.control.Button;
      import javafx.scene.layout.BorderPane;
      import javafx.stage.Stage;

      /**
       *
       * @author molloc
       */
      public class LineChartSample extends Application {
       
          @Override public void start(Stage stage) {
              stage.setTitle("Line Chart Sample");
              // Define the axes
              final NumberAxis xAxis = new NumberAxis();
              final NumberAxis yAxis = new NumberAxis();
              xAxis.setLabel("Month Number");
              // Create the chart
              final LineChart<Number,Number> lineChart =
                      new LineChart<Number,Number>(xAxis,yAxis);
              lineChart.setTitle("Stock Monitoring, 2010");
              // Define a series
              XYChart.Series series = new XYChart.Series();
              series.setName("My portfolio");
              // Populating the series with data
              series.getData().add(new XYChart.Data(1, 23));
              series.getData().add(new XYChart.Data(2, 14));
              series.getData().add(new XYChart.Data(3, 15));
              series.getData().add(new XYChart.Data(4, 24));
              series.getData().add(new XYChart.Data(5, 34));
              series.getData().add(new XYChart.Data(6, 36));
              series.getData().add(new XYChart.Data(7, 22));
              series.getData().add(new XYChart.Data(8, 45));
              series.getData().add(new XYChart.Data(9, 43));
              series.getData().add(new XYChart.Data(10, 17));
              series.getData().add(new XYChart.Data(11, 29));
              series.getData().add(new XYChart.Data(12, 25));
              
              BorderPane bdrPane = new BorderPane();
              bdrPane.setCenter(lineChart);
              
              Button removeAddBtn = new Button();
              removeAddBtn.setText("Remove/Add Series");
              removeAddBtn.setOnAction(new EventHandler<ActionEvent>() {
                  @Override
                  public void handle(ActionEvent t) {
                      ObservableList<Series<Number, Number>> data = lineChart.getData();
                      Series<Number, Number> removedSeries = data.remove(0);
                      
                      // The following causes an IllegalArgumentException: duplicate
                      // children added
                      //data.add(removedSeries);
                      
                      // The following also causes an IllegalArgumentException
                      Series<Number, Number> newSeries = new Series<>(removedSeries.getData());
                      data.add(newSeries);
                  }
              });
              bdrPane.setBottom(removeAddBtn);
              
              Scene scene = new Scene(bdrPane, 800, 600);
              lineChart.getData().add(series);
             
              stage.setScene(scene);
              stage.show();
          }
       
          public static void main(String[] args) {
              launch(args);
          }
      }

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            duke J. Duke (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Imported: