title: JavaFx: Using existing branch as newly added branch
body: <p>Hi i am working on an application related to treeView in javafx, i asked this question before  [1]: <a href="https://stackoverflow.com/questions/44500216/javafx-how-to-use-existing-branch-with-leafs-as-a-newly-add-branch">JavaFx: How to Use existing branch with leafs as a newly add branch</a> , but didn't get any answer. i tried myself and reached here </p>
  
  <pre><code>public class Main extends Application
  {
      private static TreeItem&lt;String&gt; item2;
      @SuppressWarnings("unchecked")
      @Override
      public void start(Stage primaryStage)
      {   
         BorderPane border = new BorderPane();    
          Scene scene = new Scene(border,300,300); 
          primaryStage.setTitle("BorderPane");
          primaryStage.setScene(scene);
          primaryStage.show();    
  
          TreeItem&lt;String&gt; tree = new TreeItem&lt;String&gt;("Root"); 
           item2 = new TreeItem&lt;String&gt;("poles");
          TreeItem&lt;String&gt;  item2Child1 = new TreeItem&lt;String&gt;("X");
          TreeItem&lt;String&gt; item2Child2 = new TreeItem&lt;String&gt;("Y");
          item2.getChildren().addAll(item2Child1,item2Child2);        
          //item2.setExpanded(true);    
          tree.setExpanded(true);
          tree.getChildren().addAll(item2);
          TreeView&lt;String&gt; treeView  = new TreeView&lt;String&gt;(tree);       
          treeView.setCellFactory(new Callback&lt;TreeView&lt;String&gt;,TreeCell&lt;String&gt;&gt;(){
              @Override
              public TreeCell&lt;String&gt; call(TreeView&lt;String&gt; p) {
                  return new AddMenuTreeCell();
              }
          });        
          border.setCenter(treeView);
      }    
      private static class AddMenuTreeCell extends TextFieldTreeCell&lt;String&gt; {
          private ContextMenu menu = new ContextMenu();
          private TextField textField;
  
          public AddMenuTreeCell() {
              MenuItem item1 = new MenuItem("Insert poles");            
              menu.getItems().add(item1);
              item1.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
                  @Override
                  public void handle(ActionEvent arg0) {              
                                  getTreeItem().getChildren().add(item2);                               
                  }
              });              
          }
          @Override
          public void updateItem(String item, boolean empty) {
              super.updateItem(item, empty);
              if (empty) {
                  setText(null);
                  setGraphic(null);
              } else {
                  if (isEditing()) {
                      if (textField != null) {
                          textField.setText(getString());
                      }
                      setText(null);
                      setGraphic(textField);
                  } else {
                      setText(getString());
                      setGraphic(getTreeItem().getGraphic());
                      if (!(getTreeItem().isLeaf() &amp;&amp; getTreeItem().getParent() == null)){
                          setContextMenu(menu);
                      }
                  }           
              }
          }
          private String getString() {
              return getItem() == null ? "" : getItem().toString();
          }
      }  
  }   
  </code></pre>
  
  <p>With this user can add existing branch "poles". But the problem is, user cannot add Poles inside any pole branch.  </p>
  
  <p>[Application work like this user have to use existing branch "pole" as newly added branch and can add as many poles as he want inside any branch.] </p>
  
  <p>Any good Idea will be helpful Please. </p>
  
  <p>Thank you </p>