title: Creating a custom tree with javafx
body: <p>Basically, I wanted to know if I could create a tree and custom it on javaFX...
  I tried to do it, but couldn't do anything so far with this code...</p>
  
  <pre><code>public class Main{
      ......
  
   public Main() throws Exception{
      ......    
  
     // TreeView created
      TreeView tv = (TreeView) fxmlLoader.getNamespace().get("treeview");
  
      TreeItem&lt;String&gt; rootItem = new TreeItem&lt;String&gt;("liss");
      rootItem.setExpanded(true);
      tv.setRoot(rootItem);
  
      /*for (int i = 1; i &lt; 6; i++) {
          TreeItem&lt;String&gt; item = new TreeItem&lt;String&gt; ("Message" + i);
          rootItem.getChildren().add(item);
      }
      TreeItem&lt;String&gt; item = new TreeItem&lt;String&gt; ("MessageWoot");
      rootItem.getChildren().add(item);
  */
      //tv.setEditable(true);
  
      tv.setCellFactory(new Callback&lt;TreeView&lt;String&gt;, TreeCell&lt;String&gt;&gt;() {
          @Override
          public TreeCell&lt;String&gt; call(TreeView&lt;String&gt; arg0) {
              // custom tree cell that defines a context menu for the root tree item
              return new MyTreeCell();
          }
      });
  
      stage.show();
  }
  
  //
  private static class MyTreeCell extends TextFieldTreeCell&lt;String&gt; {
      private ContextMenu addMenu = new ContextMenu();
      public boolean clickedFirstTime = false;
  
      public MyTreeCell() {
          // instantiate the root context menu
          MenuItem addMenuItem = new MenuItem("Expand");
          addMenu.getItems().add(addMenuItem);
          addMenuItem.setOnAction(new EventHandler() {
  
              public void handle(Event t) {
                  TreeItem n0 =
                          new TreeItem&lt;String&gt;("'program'");
                  TreeItem n1 =
                          new TreeItem&lt;String&gt;("&lt;identifier&gt;");
                  TreeItem n2 =
                          new TreeItem&lt;String&gt;("body");
  
                  getTreeItem().getChildren().add(n0);
                  getTreeItem().getChildren().add(n1);
                  getTreeItem().getChildren().add(n2);
  
              }
          });
      }
  
      @Override
      public void updateItem(String item, boolean empty) {
          super.updateItem(item, empty);
  
          // if the item is not empty and is a root...
          //if (!empty &amp;&amp; getTreeItem().getParent() == null &amp;&amp; this.clickedFirstTime) {
          System.out.println("UPDATEITEM -&gt; clickedFirstTime : "+this.clickedFirstTime);
          if (!this.clickedFirstTime) {
              System.out.println("WOOT");
              setContextMenu(addMenu);
              this.clickedFirstTime = true;
          }
      }
  
  }
  </code></pre>
  
  <p>And I'm questioning myself if this is the right "technology" which will solve what I'm trying to do...</p>
  
  <p>What's my objective in this?</p>
  
  <p>Firstly, I'm looking to add or delete a treeItem. I must say that a certain treeItem may be added only once or any N times, like a restriction (for example: treeItem &lt; 6 for a certain level scope and a certain path of the root of tree view).</p>
  
  <p>Secondly, make some treeItem editable and others not editable! When it is Editable, you may pop up something for the user in order to insert some input for example!</p>
  
  <p>Is it possible ?</p>
  
  <p>I saw the tutorial from <a href="https://docs.oracle.com/javafx/2/ui_controls/tree-view.htm#BABJGGGF" rel="nofollow noreferrer">https://docs.oracle.com/javafx/2/ui_controls/tree-view.htm#BABJGGGF</a> but I'm really confused with this tutorial ... I don't really understand the cell factory mechanism... The fact that he does apply to TreeView when i want only a certain TreeItem... Or how could I control that effect/behaviour ?
  I mean, I'm really really lost with TreeView. Probably, TreeView isn't what I'm looking for ...</p>
  
  <p>P.S.: I know that I cannot apply any visual effect or add menus to a tree items and that i use a cell factory mechanism to overcome this obstacle. Just I don't understand the idea and how could I do it !</p>