That method returns null
if there's no cellValueFactory
set. Besides, you should really have a model to hold this state—a TableView
is just a view. Unlike the TableViewSelectionModel
, which represents which items are selected solely in the context of the TableView
itself, a column containing CheckBox
es represents the "boolean state" of a property of the model. For example:
public class ToDoTask {
private final StringProperty name = new SimpleStringProperty(this, "name");
private final BooleanProperty complete = new SimpleBooleanProperty(this, "complete");
// constructors, getters, setters, and property-getters omitted for brevity
}
A TableView
for displaying that class could be configured like the following:
TableView<ToDoTask> table = new TableView<>();
table.setItems(...);
TableColumn<ToDoTask, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(features -> features.getValue().nameProperty());
table.getColumns().add(nameCol);
TableColumn<ToDoTask, Boolean> completeCol = new TableColumn<>("Complete");
completeCol.setCellValueFactory(features -> features.getValue().completeProperty());
completeCol.setCellFactory(CheckBoxTableCell.forTableColumn(completeCol));
table.getColumns().add(completeCol);
You would then query if a task is complete by accessing the model:
table.getItems().get(...).isComplete();
Another option to setting the cellValueFactory
is to register a Callback
with the CheckBoxTableCell
s themselves. See CheckBoxTableCell#forTableColumn(Callback)
.
Also, note that getCellObservableValue()
returns an ObservableValue
. A CheckBox
is not an ObservableValue
. If you weren't receiving null
you'd be getting a ClassCastException
.