title: JavaFX pane.getChildren().remove(child) throws IllegalArgumentException
body: <p>I'm trying to create a component which registers a listener on parent of a Line and when it changes it adds/removes a rectangle on Parent of line.</p>
  
  <p>You can see the code bellow.
  Basically I have 2 buttons</p>
  
  <ul>
  <li>Add > Adds the line to demoPane</li>
  <li>Remove > Removes the line from demoPane</li>
  </ul>
  
  <p>I also register a listener on line parent as I already explained.</p>
  
  <p>When I click on Add Button everything goes fine... The line is added and subsequently the rectangle is also added.</p>
  
  <p>When i press remove the line is removed from demoPane but on removal of rectangle, it is thrown the following exception : </p>
  
  <pre><code> -Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = AnchorPane[id=demoPane]
      at javafx.scene.Parent$2.onProposedChange(Unknown Source)
      at com.sun.javafx.collections.VetoableListDecorator.remove(Unknown Source)
      at demo.Delete.lambda$2(Delete.java:63)
  </code></pre>
  
  <p>This happens on line 63 which is where I remove rectangle from demoPane</p>
  
  <pre><code>private static void mountDemo(AnchorPane demoPane) {
          Button buttonAdd = new Button("Add");
          Button buttonRemove = new Button("Remove");
          Line line = new Line(100, 100, 400, 100);
          Rectangle rectangle = new Rectangle(20, 20);
  
          demoPane.getChildren().add(buttonAdd);
          demoPane.getChildren().add(buttonRemove);
  
          buttonAdd.setOnMouseClicked((event)-&gt;demoPane.getChildren().add(line));
          buttonRemove.setOnMouseClicked((event)-&gt;demoPane.getChildren().remove(line));
  
          line.parentProperty().addListener((observable, oldParent, newParent)-&gt;{
              if(newParent != null)
                  ((Pane)newParent).getChildren().add(rectangle);
              else
                  ((Pane)oldParent).getChildren().remove(rectangle);
          }); 
      }
  </code></pre>
  
  <p>Can anyone help me with it? What am I doing wrong?</p>