title: javafx how to get selected row data in table view with checkbox
body: <p>In this project I can't get whether the Checkbox data was selected or not in the console output.</p>
  
  <p>My code:</p>
  
  <pre><code>import java.util.Arrays;
  import java.util.Iterator;
  import java.util.List;
  
  import javafx.application.Application;
  import javafx.beans.Observable;
  import javafx.beans.property.BooleanProperty;
  import javafx.beans.property.SimpleBooleanProperty;
  import javafx.beans.property.SimpleStringProperty;
  import javafx.beans.property.StringProperty;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.collections.FXCollections;
  import javafx.collections.ListChangeListener;
  import javafx.collections.ObservableList;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.geometry.Pos;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.control.TableCell;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableColumn.CellEditEvent;
  import javafx.scene.control.TablePosition;
  import javafx.scene.control.TableView;
  import javafx.scene.control.cell.CheckBoxTableCell;
  import javafx.scene.control.cell.PropertyValueFactory;
  import javafx.scene.layout.BorderPane;
  import javafx.stage.Stage;
  import javafx.util.Callback;
  
  public class TableWithCheckbox extends Application {
     final TableView&lt;Player&gt; table = new TableView&lt;Player&gt;();
      ObservableList&lt;Player&gt; data ;
    @Override
    public void start(Stage primaryStage) {
      final BorderPane root = new BorderPane();
  
      table.setItems(createData());
      final TableColumn&lt;Player, String&gt; firstNameColumn = new TableColumn&lt;&gt;("First Name");
      final TableColumn&lt;Player, String&gt; lastNameColumn = new TableColumn&lt;&gt;("Last Name");
      final TableColumn&lt;Player, Boolean&gt; injuredColumn = new TableColumn&lt;&gt;("Injured");
      firstNameColumn.setCellValueFactory(new PropertyValueFactory&lt;Player, String&gt;("firstName"));
      lastNameColumn.setCellValueFactory(new PropertyValueFactory&lt;Player, String&gt;("lastName"));
      injuredColumn.setCellValueFactory(new PropertyValueFactory&lt;Player, Boolean&gt;("injured"));
      final Callback&lt;TableColumn&lt;Player, Boolean&gt;, TableCell&lt;Player, Boolean&gt;&gt; cellFactory = CheckBoxTableCell.forTableColumn(injuredColumn);
      injuredColumn.setCellFactory(new Callback&lt;TableColumn&lt;Player, Boolean&gt;, TableCell&lt;Player, Boolean&gt;&gt;() {
        @Override
        public TableCell&lt;Player, Boolean&gt; call(TableColumn&lt;Player, Boolean&gt; column) {
          TableCell&lt;Player, Boolean&gt; cell = cellFactory.call(column);
          cell.setAlignment(Pos.CENTER);
          return cell ;
        }
      });
      injuredColumn.setCellFactory(cellFactory);
      injuredColumn.setEditable(true);
  
      // This appears not to get invoked:
      injuredColumn.setOnEditCommit(new EventHandler&lt;TableColumn.CellEditEvent&lt;Player,Boolean&gt;&gt;() {
        @Override
        public void handle(CellEditEvent&lt;Player, Boolean&gt; event) {
          System.out.println("Edit commit");
        }
      });
  
      table.setEditable(true);
      table.getColumns().addAll(firstNameColumn, lastNameColumn, injuredColumn);
  
      root.setCenter(table);
  
      Button button = new Button("Dump info");
      button.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
        @Override
        public void handle(ActionEvent event) {
          for (Player p : table.getItems()) {
            System.out.println(p);
          }
          System.out.println();
        }
      }); Button button1 = new Button("add");
      button1.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
        @Override
        public void handle(ActionEvent event) {
    data.add(new Player("hbghj", "hjhbbccc77", true));
        }
      });
      root.setBottom(button); 
      root.setTop(button1);
      primaryStage.setScene(new Scene(root, 400, 600));
      primaryStage.show();    
    }
  
    public static void main(String[] args) {
      launch(args);
    }
  
    private ObservableList&lt;Player&gt; createData() {
      List&lt;Player&gt; players = Arrays.asList(
          new Player("Hugo" ,"Lloris", false),
          new Player("Brad", "Friedel", false),
          new Player("Kyle", "Naughton", false),
          new Player("Younes", "Kaboul", true),
          new Player("Benoit", "Assou-Ekotto", false),
          new Player("Jan", "Vertonghen", false),
          new Player("Michael", "Dawson", false),
          new Player("William", "Gallas", true),
          new Player("Kyle", "Walker", false),
          new Player("Scott", "Parker", false),
          new Player("Mousa", "Dembele", false),
          new Player("Sandro", "Cordeiro", true),
          new Player("Tom", "Huddlestone", false),
          new Player("Gylfi","Sigurdsson", false),
          new Player("Gareth", "Bale", false),
          new Player("Aaron", "Lennon", false),
          new Player("Jermane", "Defoe", false),
          new Player("Emmanuel", "Adebayor", true)
      );
    data =  FXCollections.&lt;Player&gt;observableArrayList( 
          new Callback&lt;Player, Observable[]&gt;() {
            @Override
            public Observable[] call(Player player) {
              return new Observable[]{player.injuredProperty()};
            }
          }
      );
      data.addAll(players);
      data.addListener(new ListChangeListener&lt;Player&gt;() {
        @Override
        public void onChanged(
            javafx.collections.ListChangeListener.Change&lt;? extends Player&gt; change) {
          System.out.println("List changed");
          while (change.next()) {
            if (change.wasUpdated()) {
                System.out.println("What code a do here");
          System.out.println();  }
          }
        }
      });
       table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
              @Override
              public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
                  //Check whether item is selected and set value of selected item to Label
                  if (table.getSelectionModel().getSelectedItem() != null) {
                      TableView.TableViewSelectionModel selectionModel = table.getSelectionModel();
                      ObservableList selectedCells = selectionModel.getSelectedCells();
                      TablePosition tablePosition = (TablePosition) selectedCells.get(0);
                      Object val = tablePosition.getTableColumn().getCellData(newValue);
                      System.out.println("Selected Value " + val);
                      System.out.println("Selected row " + newValue);
         }
              }
          });
      return data ;
    }
  
    public static class Player {
      private final StringProperty firstName ;
      private final StringProperty lastName ;
      private final BooleanProperty injured ;
      Player(String firstName, String lastName, boolean international) {
        this.firstName = new SimpleStringProperty(this, "firstName", firstName);
        this.lastName = new SimpleStringProperty(this, "lastName", lastName);
        this.injured = new SimpleBooleanProperty(this, "injured", international);
      }
      public String getFirstName() { return firstName.get(); }
      public void setFirstName(String firstName) { this.firstName.set(firstName);}
      public StringProperty firstNameProperty() { return firstName ; }
      public String getLastName() { return lastName.get(); }
      public void setLastName(String lastName) { this.lastName.set(lastName); }
      public StringProperty lastNameProperty() { return lastName ; }    
      public boolean isInjuredl() { return injured.get(); }
      public void setInjured(boolean international) { this.injured.set(international); }
      public BooleanProperty injuredProperty() { return injured ; }
      @Override
      public String toString() {
        return firstName.get() + " " + lastName.get() + (injured.get()?" (injured)":"");
      }
    }
  }
  </code></pre>
  
  <p>it gives me the output like</p>
  
  <pre><code>Selected row: Gareth    Bale
  </code></pre>
  
  <p>but it doesn't tell weather Checkbox is selected or not on function:</p>
  
  <pre><code>public void onChanged
  </code></pre>
  
  <p>I need an output like:</p>
  
  <pre><code> Selected row: Gareth    Bale   true
  </code></pre>
  
  <p>or</p>
  
  <pre><code> Selected row: Gareth    Bale   false
  </code></pre>