title: Programmatically edit TreeView/TreeItem
body: <p><strong>Edit #2</strong>: Since it looks like a bug i already posted a bug report in the <a href="https://bugs.openjdk.java.net/browse/JDK-8089497" rel="nofollow">javaFx-jira</a>. You have to have an account to have access to the issue. I will keep this post up-to-date, if there is new information.</p>
  
  <p><strong>Original post:</strong>
  I have a simple UI with a button and a TreeView. If the button gets pressed there should be a new item added to the TreeView. This item should be editable as soon as it appears in the tree.</p>
  
  <pre><code>import javafx.fxml.FXML;
  import javafx.scene.control.TreeItem;
  import javafx.scene.control.TreeView;
  import javafx.scene.control.cell.TextFieldTreeCell;
  import javafx.util.converter.DefaultStringConverter;
  
  public class ClientController {
  
      @FXML
      private TreeView&lt;String&gt; tree;
  
      public void initialize() {
          tree.setEditable(true);
          tree.setCellFactory(p -&gt; new TextFieldTreeCell&lt;&gt;(new DefaultStringConverter()));
  
          TreeItem&lt;String&gt; root = new TreeItem&lt;&gt;();
          root.setValue("Items");
          root.setExpanded(true);
  
          tree.setRoot(root);
      }
  
      @FXML
      public void createItem() {
          TreeItem&lt;String&gt; newItem = new TreeItem&lt;&gt;();
          newItem.setValue("Item " + tree.getExpandedItemCount());
  
          tree.getRoot().getChildren().add(newItem);
          tree.requestFocus();
          tree.getSelectionModel().select(newItem);
          tree.edit(newItem);
      }
  }
  </code></pre>
  
  <p>The CellFactory i am using is <a href="https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/cell/TextFieldTreeCell.html" rel="nofollow">part of the JavaFX api</a>.</p>
  
  <p>If I have a look at the api-documentation (<a href="https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TreeView.html#edit-javafx.scene.control.TreeItem-" rel="nofollow">TreeView#edit</a>) there is not much to do on my site. </p>
  
  <p>Many examples I found via google like <a href="http://docs.oracle.com/javafx/2/ui_controls/tree-view.htm#BABFCCCG" rel="nofollow">this</a> create a context-menu for each TreeItem. Useful, but not exactly what i want, right now.</p>
  
  <p>If I select/double-click the item in the UI I am able to edit any previously created and existing TreeItem. Do I miss something?</p>
  
  <p><strong>Edit #1:</strong></p>
  
  <p>If the createItem method gets changed to the following code:</p>
  
  <pre><code>@FXML
  public void createItem() throws InterruptedException {
      TreeItem&lt;String&gt; newItem = new TreeItem&lt;&gt;();
      newItem.setValue("Item " + this.tree.getExpandedItemCount());
  
      this.tree.getRoot().getChildren().add(newItem);
      this.tree.requestFocus();
      this.tree.getSelectionModel().select(newItem);
      Thread.sleep(100);
      Platform.runLater(new Runnable() {
          @Override
          public void run() {
              SimpleController.this.tree.edit(newItem);
          }
      });
  }
  </code></pre>
  
  <p>every new item is correctly marked for edit (the created textfield and its content are selected). 
  Using <a href="https://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#runLater-java.lang.Runnable-" rel="nofollow">Platform.runLater</a> without <em>Thread.sleep</em> doesnt work and <em>Thread.sleep</em> without <em>runLater</em> neither.</p>
  
  <p>This simply does not feel right. What is my problem? </p>
  
  <p>I provide a very small example (eclipse) project:
  <a href="https://www.dropbox.com/s/duos0ynw4rqp3yn/Test.zip?dl=0" rel="nofollow">https://www.dropbox.com/s/duos0ynw4rqp3yn/Test.zip?dl=0</a>
  containing The main-method, the FXML-File and the "problematic" controller.</p>