title: Capture values from a TextField within a table cell
body: <p>Scenario: Im passing in comma separated values to a table. 2 columns, one with the original value, one with a text field with the values populated inside. They are lined up so I can add/change values, and copy the changes to a string.</p>
  
  <p>I cannot figure out how to capture the TextField data after they are put in the table. Code below:</p>
  
  <p>Main:</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.stage.Stage;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  
  public class Main extends Application {
  
  
      public static void main(String[] args) {
          launch(args);
      }
  
      @Override
      public void start(Stage primaryStage) {
          try {
              Parent root = FXMLLoader.load(getClass().getResource("/application/MainFxml.fxml")); //this is the file that 
              Scene scene = new Scene(root,800,800); ////100,100 is width and height of window
              scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
              primaryStage.setScene(scene);
              primaryStage.show();
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>import java.net.URL;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.EnumSet;
  import java.util.ResourceBundle;
  import javafx.beans.property.SimpleObjectProperty;
  import javafx.beans.property.SimpleStringProperty;
  import javafx.beans.property.StringProperty;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.geometry.Pos;
  import javafx.scene.control.Button;
  import javafx.scene.control.ListView;
  import javafx.scene.control.TableCell;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableView;
  import javafx.scene.control.TextField;
  import javafx.scene.control.TableColumn.CellEditEvent;
  import javafx.scene.control.cell.PropertyValueFactory;
  import javafx.scene.control.cell.TextFieldTableCell;
  import javafx.scene.layout.HBox;
  
  public class MainFxmlController implements Initializable {
  
      public static int count=-1;
  
      @FXML public TableView&lt;tableClass&gt; table = new TableView&lt;tableClass&gt;();
      @FXML private TableColumn&lt;tableClass, String&gt;col1;
      @SuppressWarnings({ "unchecked", "rawtypes" })
      @FXML public TableColumn&lt;tableClass, Row&gt; col2 = new TableColumn("Row");
  
      @FXML public TextField txt;
      @FXML public Button btn, btn2;
      @FXML public ListView&lt;String&gt; listView = new ListView&lt;String&gt;();
      public static ArrayList&lt;String&gt; input = new ArrayList&lt;String&gt;();
  
      final HBox hb = new HBox();
      public ObservableList&lt;tableClass&gt; obList = FXCollections.observableArrayList();  // each column contains an observable list object 
      public ObservableList&lt;tableClass&gt; loadTable(){
          return obList;   //return data object
      }////END loadData
  
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
              table.setEditable(true);
              col1.setCellValueFactory(cellData -&gt; cellData.getValue().getCol1());  
  
  
              col2.setCellFactory((param) -&gt; new TextFieldCell&lt;tableClass, Row&gt;(EnumSet.allOf(Row.class)));
              col2.setCellValueFactory(new PropertyValueFactory&lt;tableClass, Row&gt;("Row"));
              col2.setOnEditCommit(
                      new EventHandler&lt;CellEditEvent&lt;tableClass, Row&gt;&gt;() {
                          @Override
                          public void handle(CellEditEvent&lt;tableClass, Row&gt; t) {
                              ((tableClass) t.getTableView().getItems().get(
                                  t.getTablePosition().getRow())
                                  ).setCol2(t.getNewValue());
                          }
                      }
                  );
  
              tableClass Entry = new tableClass(" ", Row.Row1); //create the table using getters/setters from Table Class
              table.getItems().addAll(Entry);         
              table.setItems(loadTable());
  
              col1.setCellFactory(TextFieldTableCell.&lt;tableClass&gt;forTableColumn()); //Makes the columns themselves editable
              col1.setOnEditCommit(
                      new EventHandler&lt;CellEditEvent&lt;tableClass, String&gt;&gt;() {
                          @Override
                          public void handle(CellEditEvent&lt;tableClass, String&gt; t) {
                              ((tableClass) t.getTableView().getItems().get( t.getTablePosition().getRow())).setCol1(t.getNewValue());
                          }              
                      }
                      );
              col1.setStyle( "-fx-alignment: BOTTOM-RIGHT;"); //to alight text next to textArea
  
              txt.setText("fsad,0,0,gfds,43,4,4,fdsg,rtewrtwe,0,67,3,4,4,,4,44,,4");  //TO BE ROMOVED UPON COMPLETION
  
      }//end initialize
  
      public void buttonAction(ActionEvent e){
          if(txt.getText() != ""){
              System.out.println(txt.getText());
              ArrayList&lt;String&gt; myList = new ArrayList&lt;String&gt;(Arrays.asList(txt.getText().split(",")));
              input = myList;     
          }
  
          for(int i =0; i&lt; input.size(); i++){
              Row.Row1.equals(input.get(i).toString());
              obList.add(new tableClass(input.get(i).toString(), Row.Row1));
          }//end for
  
      }//end buttonAction
  
  
      public void captureText(ActionEvent e){
  
          /*
           * HERE I NEED TO CAPTURE VALUES FROM THE TEXT FIELDS
           * IN COL2
           */
  
      }
  
  
  
      public static enum Row { //enum for the dxTable radio button  if you want any other options, add another value and another radio buttton will be populated
          Row1;
      }
  
  
      public static class TextFieldCell&lt;S,T extends Enum&lt;T&gt;&gt; extends TableCell&lt;S,T&gt;{
  
          private EnumSet&lt;T&gt; enumeration;
  
          public TextFieldCell(EnumSet&lt;T&gt; enumeration) {
              this.enumeration = enumeration;
          }
  
          @Override
          protected void updateItem(T item, boolean empty)
          {
              super.updateItem(item, empty);
              if (!empty) 
              {
                  // gui setup
                  HBox hb = new HBox(7);
                  hb.setAlignment(Pos.CENTER);
  
  
                  // create a radio button for each 'element' of the enumeration
                  for (Enum&lt;T&gt; enumElement : enumeration) {
                          try{
                          TextField textField = new TextField(input.get(count));
                          textField.setUserData(enumElement);
                          hb.getChildren().add(textField);
                          }catch(IndexOutOfBoundsException e){}
                  }
  
                  // issue events on change of the selected radio button
  
                  setGraphic(hb);
                  count++;
              } //end if
              else
                  setGraphic(null);
          }//updateItem
      }//END TextFieldCell class
  
  
      public static class tableClass{              ///table object with getters and setters.
          public final SimpleStringProperty col1;
          public final SimpleObjectProperty&lt;Row&gt; col2 = new SimpleObjectProperty&lt;Row&gt;();      
  
          public tableClass(String col1, Row t) {   //uses an enum for the second type
            this.col1 = new SimpleStringProperty(col1);
            this.col2.setValue(t);
          }
  
          public StringProperty getCol1() {
              return col1;
          }
  
          public void setCol1(String i) {
              col1.set(i);
          }
  
          public void setCol2(Row t) {
              col2.set(t);
          }       
  
          public Row getCol2() {
              return col2.get();
          }
          public String getCol2(int index) {
  
              return "";
          }
  
        }//end table class
  }//end controller
  </code></pre>
  
  <p>FXML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.TableColumn?&gt;
  &lt;?import javafx.scene.control.TableView?&gt;
  &lt;?import javafx.scene.control.TextField?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;AnchorPane prefHeight="800.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainFxmlController"&gt;
     &lt;children&gt;
        &lt;Button fx:id="btn" layoutX="723.0" layoutY="26.0" mnemonicParsing="false" onAction="#buttonAction" text="btn" /&gt;
        &lt;TextField fx:id="txt" layoutX="41.0" layoutY="26.0" prefHeight="25.0" prefWidth="647.0" /&gt;
        &lt;TableView fx:id="table" editable="true" layoutX="41.0" layoutY="106.0" prefHeight="588.0" prefWidth="451.0"&gt;
          &lt;columns&gt;
            &lt;TableColumn fx:id="col1" prefWidth="75.0" text="C1" /&gt;
            &lt;TableColumn fx:id="col2" prefWidth="114.0" text="C2" /&gt;
          &lt;/columns&gt;
        &lt;/TableView&gt;
        &lt;Button fx:id="btn2" layoutX="507.0" layoutY="669.0" mnemonicParsing="false" onAction="#captureText" text="Button" /&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>