title: Javafx 8 tableview add row and focus
body: <p>I would like to add a functionality of dirty/validated row to the tableview control, where the user will be forced to edit the newly added row, otherwise it will be removed.
  What is best way to focus the first cell (the editor of which is <code>ComboboxTableCell</code>) of the newly added row. The new row is added with 
  <code>tableview.getItems().getItems().addAll(new Item())</code></p>
  
  <p>Thanks in advance.</p>
  
  <p>Here is the code of the controller and of the view :</p>
  
  <pre><code>public class SaleDetailController implements Initializable {
      @FXML
      private TableView&lt;SaleDetail&gt; saleDetailTable;
  
      @FXML
      private TableColumn&lt;SaleDetail, Product&gt; productColumn;
  
      @FXML
      private TableColumn&lt;SaleDetail, Integer&gt; quantityColumn;
  
      public void initialize(URL location, ResourceBundle resources) {
      productColumn.setCellFactory(ComboBoxTableCell.forTableColumn(FXCollections.observableArrayList(DaoFactory.getProductDao().getAll())));
          productColumn.setCellValueFactory(cellData -&gt; new SimpleObjectProperty(cellData.getValue().getProduct()));
          quantityColumn.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
          quantityColumn.setCellValueFactory(cellData -&gt; new SimpleObjectProperty(cellData.getValue().getQuantity()));
      }
  
     @FXML
     private void handleRemoveProductButton(ActionEvent event) {
         int selectedIndex = saleDetailTable.getSelectionModel().getSelectedIndex();
          if (selectedIndex &gt;= 0) {
              saleDetailTable.getItems().remove(selectedIndex);
          }
      }
  
      @FXML
      private void handleAddProductButton(ActionEvent event) {
          SaleDetail saleDetail = new SaleDetail();
          saleDetail.setQuantity(0);
          Product product = new Product();
          saleDetail.setProduct(product);
          saleDetailTable.getItems().addAll(saleDetail);
  
          saleDetailTable.getSelectionModel().selectLast();
          saleDetailTable.requestFocus();
          saleDetailTable.getFocusModel().focus(saleDetailTable.getItems().size() - 1);
          saleDetailTable.edit(saleDetailTable.getFocusModel().getFocusedIndex(), productColumn);
      }
  }
  </code></pre>
  
  <p><br/></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.geometry.Insets?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.text.Font?&gt;
  
  &lt;VBox fx:id="saleDetailView" minWidth="500.0" spacing="15.0" style="-fx-border-style: solid; -fx-border-color: #D3D3D3;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zpavel.controller.SaleDetailController"&gt;
      &lt;children&gt;
          &lt;HBox spacing="10.0"&gt;
              &lt;children&gt;
                  &lt;Label text="Products*" /&gt;
                  &lt;Button mnemonicParsing="false" onAction="#handleAddProductButton" text="Add" /&gt;
                  &lt;Button mnemonicParsing="false" onAction="#handleRemoveProductButton" text="Remove" /&gt;
              &lt;/children&gt;
          &lt;/HBox&gt;
          &lt;TableView fx:id="saleDetailTable" editable="true"&gt;
              &lt;columns&gt;
                  &lt;TableColumn fx:id="productColumn" prefWidth="75.0" text="Product*" /&gt;
                  &lt;TableColumn fx:id="quantityColumn" prefWidth="75.0" text="Quantity*" /&gt;
              &lt;/columns&gt;
              &lt;columnResizePolicy&gt;
                  &lt;TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /&gt;
              &lt;/columnResizePolicy&gt;
          &lt;/TableView&gt;
      &lt;/children&gt;
      &lt;padding&gt;
          &lt;Insets bottom="15.0" left="15.0" right="15.0" top="15.0" /&gt;
      &lt;/padding&gt;
  &lt;/VBox&gt;
  </code></pre>