title: JavaFX 2 TreeView change Focus from TreeView TreeCell
body: <p>Hello I´m trying to implement a TreeView which is capable of creating a new Element via ContextMenu in the TreeView. However I want the new element to be focused to call the startEdit() method and give it a name myself. Is there any possible way to do so...</p>
  
  <p>Here´s my code: </p>
  
  <pre><code>public class NotesBrowser extends TreeView&lt;String&gt;
  {
      public NotesBrowser()
      {
      this.setEditable(true);
  
      IconView rootIcon = new IconView(Resources.rootImage);
      rootIcon.setSize(28.5, 28.5);
  
      ExtendedTreeItem treeRoot = new ExtendedTreeItem("Notes", true);
      treeRoot.setGraphic(rootIcon);
      treeRoot.setIsRoot(true);
      setRoot(treeRoot);
  
      this.setId("menubar_Notes");
      this.setShowRoot(true);
      this.setRoot(treeRoot);
  
      this.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 TextFieldTreeCellImpl();
          }
      });
  
      treeRoot.setIsRoot(true);
      treeRoot.setExpanded(true);
      }
  
      /**
       * TextFieldTreeCellImpl handles actions on TreeView items Core functions: -
       * Right Click on Folder -&gt; create Folder, add Note, delete, rename - Right
       * Click on Note -&gt; add Note, delete, rename
       * 
       * @author Googles
       * @credits Oracle TreeView
       *          Samples(http://docs.oracle.com/javafx/2/ui_controls
       *          /TreeViewSample.java.html)
       * 
       */
      public final class TextFieldTreeCellImpl extends TreeCell&lt;String&gt;
      {
      private TextField textField;
  
      private ContextMenu noteMenu = new ContextMenu();
      private ContextMenu folderMenu = new ContextMenu();
  
      @SuppressWarnings({ "rawtypes", "unchecked" })
      public TextFieldTreeCellImpl()
      {
          // Initialize MenuItems
          MenuItem addFolder = new MenuItem("Create Folder");
          MenuItem addNote = new MenuItem("Create Note");
          MenuItem delete = new MenuItem("Delete");
          MenuItem rename = new MenuItem("Rename");
  
          // Add MenuItem to dedicated ContextMenus
          noteMenu.getItems().addAll(rename, delete);
          folderMenu.getItems().addAll(addNote, addFolder, rename, delete);
  
          // Set MenuItem actions
          addNote.setOnAction(new EventHandler()
          {
          public void handle(Event t)
          {
              ExtendedTreeItem newNote = new ExtendedTreeItem("New Note", false);
              getTreeItem().getChildren().add(newNote);
              getTreeItem().setExpanded(true);
  
          }
          });
  
          rename.setOnAction(new EventHandler()
          {
          @Override
          public void handle(Event t)
          {
              startEdit();
          }
          });
  
      }
  
      /**
       * Creates TextField on TreeCell Element to rename it
       */
      @Override
      public void startEdit()
      {
          if (!((ExtendedTreeItem) getTreeItem()).isRoot())
          {
          super.startEdit();
          if (textField == null)
          {
              createTextField();
          }
          setText(null);
          setGraphic(textField);
          textField.selectAll();
          }
      }
  
      /**
       * Cancel Edit; obvious comment is obvious...
       */
      @Override
      public void cancelEdit()
      {
          super.cancelEdit();
  
          setText((String) getItem());
          setGraphic(getTreeItem().getGraphic());
      }
  
      /**
       * Updates TreeCell to new name and attach ContextMenus to different
       * TreeCells
       */
      @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 (((ExtendedTreeItem) getTreeItem()).isDirectory())
              {
              setContextMenu(folderMenu);
              }
              else if (!((ExtendedTreeItem) getTreeItem()).isDirectory())
              {
              setContextMenu(noteMenu);
              }
          }
          }
      }
  
      /**
       * Initializes TextField and adds onClick actions to handle commit /
       * cancel
       */
      private void createTextField()
      {
          textField = new TextField(getString());
          textField.setOnKeyReleased(new EventHandler&lt;KeyEvent&gt;()
          {
          @Override
          public void handle(KeyEvent t)
          {
              if (t.getCode() == KeyCode.ENTER)
              {
              commitEdit(textField.getText());
              }
              else if (t.getCode() == KeyCode.ESCAPE)
              {
              cancelEdit();
              }
          }
          });
      }
  
      private String getString()
      {
          return getItem() == null ? "" : getItem().toString();
      }
      }
  }
  </code></pre>