title: Javafx: TreeView adding items with right order
body: <p>i asked this question before but did'nt get any response! <a href="https://stackoverflow.com/questions/44779079/javafx-adjusting-treeview-with-right-order">Javafx: Adjusting TreeView with right order</a>. Please i need help to solve this problem. here is code </p>
  
  <pre><code>public class Main extends Application
  {       public static void main(String[] args)
              {launch(args);}
  
      static final Map&lt;TreeItem&lt;String&gt;, BorderPane&gt; map = new HashMap();
      @Override
      public void start(Stage primaryStage)
      {        
          BorderPane root = new BorderPane();
          TreeItem&lt;String&gt; tree = new TreeItem&lt;String&gt;("Main System");
          TreeItem&lt;String&gt; item1 = new TreeItem&lt;String&gt;("Roots");
          TreeView&lt;String&gt; treeView = new TreeView&lt;String&gt;(tree); 
          treeView.setOnMouseClicked((event)-&gt;{
             TreeItem&lt;String&gt; TreeItem = (TreeItem&lt;String&gt;)treeView.getSelectionModel().getSelectedItem();
             if(TreeItem.getValue().equals("Roots"))
              {              
                 root.setCenter(getRootsPanel());
              }
             else
              {
                 root.setCenter(map.get(TreeItem));
              }
          });
  
          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();
              }
          });
          tree.setExpanded(true);
          root.setLeft(treeView);
          tree.getChildren().add(item1);
  
          Scene scene = new Scene(root, 700, 500);
          primaryStage.setTitle("Tree View");
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
     TreeItem&lt;String&gt; addNewTreeItem(String name){ 
         TreeItem TreeItem = new TreeItem(name);
         return TreeItem;
      } 
     private static class AddMenuTreeCell extends TextFieldTreeCell&lt;String&gt; {
          private ContextMenu menu = new ContextMenu();            
          private TextField textField;
  
          public AddMenuTreeCell() {
              MenuItem newitem1 = new MenuItem("Insert Roots");
              MenuItem newitem2 = new MenuItem("Insert Coordinates");
              menu.getItems().addAll(newitem1,newitem2);
              newitem1.setOnAction(new EventHandler&lt;ActionEvent&gt;() {              
                  @Override
                  public void handle(ActionEvent arg0) {                      
                      TreeItem&lt;String&gt; item3 = new TreeItem&lt;String&gt;("Roots");
                         //     item3.getChildren().clear();
                              getTreeItem().getChildren().add(item3);            
                  }    
              });   
              newitem2.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
                  @SuppressWarnings("unchecked")
                  @Override
                  public void handle(ActionEvent arg0) {
                      TreeItem&lt;String&gt; newLeaf =  new TreeItem&lt;String&gt;("Coordinates");
                                  TreeItem&lt;String&gt; uxItem1 = new TreeItem("X");
                                  map.put(uxItem1, getrightPane1());
                                  TreeItem&lt;String&gt; uyItem1 = new TreeItem("y");
                                  map.put(uyItem1, getrightPane1());
                                  newLeaf.getChildren().addAll(uxItem1,uyItem1);
                                  getTreeItem().getChildren().add(newLeaf);
                  }
              });
          }
  
          @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(item);
                      }
                      setText(null);
                      setGraphic(textField);
                  } else {
                      setText(item);
                      setGraphic(getTreeItem().getGraphic());
                      if (!(getTreeItem().isLeaf() &amp;&amp; getTreeItem().getParent() == null)){
                          setContextMenu(menu);
                      }
                  }           
              }
          }  
      }
  
      private static BorderPane getrightPane1() {              
          TextField textf1 = new TextField();
          TextField textf2 = new TextField();
          BorderPane root1 = new BorderPane(); 
          VBox vbox = new VBox(20);
          vbox.setPadding(new Insets(10));        
          HBox h1 = new HBox(7);
          HBox h2 = new HBox(7); 
  
          textf1.setPrefWidth(100);
          textf1.setPromptText("Enter Height");   
          textf1.setOnKeyReleased(new EventHandler&lt;KeyEvent&gt;(){
              @Override
              public void handle(KeyEvent event)
              {  
                  if(textf1.getText().length() &gt; 0 &amp;&amp; textf2.getText().length() &gt; 0)
                  {
                     Rectangle rect1 = new Rectangle();
                      rect1.setHeight(Double.parseDouble(textf1.getText()));
                      rect1.setWidth(Double.parseDouble(textf2.getText()));
                      rect1.setFill(null);
                      rect1.setStroke(Color.RED);
                      root1.setCenter(rect1);
                  }
              }
          });
          textf2.setPrefWidth(100);
          textf2.setPromptText("Enter Width");
          textf2.setOnKeyReleased(new EventHandler&lt;KeyEvent&gt;(){
              @Override
              public void handle(KeyEvent event)
              {
                  if(textf1.getText().length() &gt; 0 &amp;&amp; textf2.getText().length() &gt; 0)
                  {
                      Rectangle rect2 = new Rectangle();
                      rect2.setHeight(Double.parseDouble(textf1.getText()));
                      rect2.setWidth(Double.parseDouble(textf2.getText()));
                      rect2.setFill(null);
                      rect2.setStroke(Color.RED);
                      root1.setCenter(rect2);
                  }
              }
          });
  
          if(textf1.getText().length() &gt; 0 &amp;&amp; textf2.getText().length() &gt; 0 &amp;&amp; root1.getCenter() == null)
          {
              Rectangle rect = new Rectangle();
              rect.setHeight(Double.parseDouble(textf1.getText()));
              rect.setWidth(Double.parseDouble(textf2.getText()));
              rect.setFill(null);
              rect.setStroke(Color.RED);
              root1.setCenter(rect);
          }
  
          h1.getChildren().addAll(new Label("Y1:"), textf1);
          h2.getChildren().addAll(new Label("X1:"), textf2);
          vbox.getChildren().addAll(h1, h2);        
          root1.setLeft(vbox);
          return root1;
      } 
  
      private static BorderPane getRootsPanel() { 
          BorderPane root2 = new BorderPane(); 
          HBox hbox = new HBox(10);
          hbox.setPadding(new Insets(40));
          hbox.setAlignment(Pos.TOP_CENTER);
  
          List&lt;BorderPane&gt; listBordePane = new ArrayList(map.values());
          for(BorderPane element : listBordePane)
          {          
              Node node = element.getCenter();
              if(node instanceof Rectangle)
              {
                  Rectangle rect1 = ((Rectangle)node);
                  Rectangle rect2 = new Rectangle();
                  rect2.setWidth(rect1.getWidth());
                  rect2.setHeight(rect1.getHeight());
                  rect2.setFill(rect1.getFill());
                  rect2.setStroke(rect1.getStroke());
                  Platform.runLater(()-&gt;{hbox.getChildren().add(rect2);});               
              }
          }
          Platform.runLater(()-&gt;{root2.setLeft(hbox);});
          return root2;
      } 
  }
  </code></pre>
  
  <p>TreeView where user can add roots and its coordinates. coordinates generates rectangles and rectangles are shown horizontally in root.</p>
  
  <p>This is how code should work "<strong>Every root should show all its coordinate's rectangles that are inside that <code>root</code> or inside its sub root.</strong> " Simply saying if user has 2 roots, each should have its own coordinated rectangles, if both has sub roots that rectangles should be added. </p>
  
  <p>due to this <code>if(TreeItem.getValue().equals("Roots"))</code>  i got all rectangles on every root that is wrong.  i tried this also <code>if(TreeItem == item1)</code> which is wrong. </p>
  
  <p>I will be very thankful if you look at this problem of mine. thank you</p>