title: How to get values of multiple selected rows in TableView using Javafx?
body: <p>I have tried the following code for TableView Multiple Selection. I just want all the ID's of selected row in an ArrayList. But whenever I select rows instead of showing ID, complete row values are displayed.</p>
  
  <pre><code>package tabletest;
  
  import java.util.ArrayList;
  import java.util.ListIterator;
  import javafx.application.Application;
  import javafx.beans.property.ReadOnlyStringWrapper;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.event.EventHandler;
  import javafx.scene.Scene;
  import javafx.scene.control.SelectionMode;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableView;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;
  
  /**
   *
   * @author vikassingh
   */
  public class TableTest extends Application {
  
      @Override
      public void start(Stage primaryStage) {
          TableView&lt;ObservableList&lt;String&gt;&gt; tableLocal = new TableView&lt;&gt;();
          tableLocal.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  
          TableColumn&lt;ObservableList&lt;String&gt;, String&gt; idCol = new TableColumn&lt;&gt;("ID");
          TableColumn&lt;ObservableList&lt;String&gt;, String&gt; nameCol = new TableColumn&lt;&gt;("Customer Name");
  
          tableLocal.getColumns().addAll(nameCol);
  
          //idCol
          idCol.setCellValueFactory(cellData -&gt; {
              ObservableList&lt;String&gt; rowValue = cellData.getValue();
              String cellValue = rowValue.get(0);
              return new ReadOnlyStringWrapper(cellValue);
          });
  
          //nameCol
          nameCol.setCellValueFactory(cellData -&gt; {
              ObservableList&lt;String&gt; rowValue = cellData.getValue();
              String cellValue = rowValue.get(1);
              return new ReadOnlyStringWrapper(cellValue);
          });
  
          tableLocal.setOnMousePressed(new EventHandler&lt;MouseEvent&gt;() {
              @Override
              public void handle(MouseEvent event) {
                  ObservableList selectedItems = tableLocal.getSelectionModel().getSelectedItems();
                  System.out.println(selectedItems);
  
                  // TEST
                  ArrayList&lt;String&gt; selectedIDs = new ArrayList&lt;String&gt;();
                  for (int i = 0; i &lt; selectedItems.size(); i++) {
                      ObservableList&lt;String&gt; innerSelectedItems = FXCollections.observableArrayList(selectedItems.get(i).toString());
  
                      for (String ID : innerSelectedItems) {
                          selectedIDs.add(ID);
                      }
  
                  }
  
                  ListIterator&lt;String&gt; iterator = selectedIDs.listIterator();
                  while (iterator.hasNext()) {
                      System.out.println(iterator.next());
                  }
                  // END TEST
              }
          });
  
          ObservableList&lt;String&gt; row1 = FXCollections.observableArrayList();
          row1.add("1");
          row1.add("Cust One");
          ObservableList&lt;String&gt; row2 = FXCollections.observableArrayList();
          row2.add("2");
          row2.add("Cust Two");
          ObservableList&lt;String&gt; row3 = FXCollections.observableArrayList();
          row3.add("3");
          row3.add("Cust Three");
          ObservableList&lt;ObservableList&lt;String&gt;&gt; filteredDataLocal = FXCollections.observableArrayList();
          filteredDataLocal.addAll(row1, row2, row3);
          tableLocal.setItems(filteredDataLocal);
  
          VBox root = new VBox();
          root.getChildren().addAll(tableLocal);
          Scene scene = new Scene(root, 300, 300);
  
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
  }
  </code></pre>