selectedProperty inherits from Cell , and it simply indicates whether Cell selected in the user interface component. Since you probably have no cell selection on your TableView , the cell will never be selected. In any case, this is not what you are looking for; You want to know if CheckBox selected, not Cell .
The trick here is to use the selectedStateCallback property for CheckBoxTableCell . This is a function that displays the cell index in BooleanProperty . That BooleanProperty bound bi-directionally to the selected state of the flag.
If your column represents the actual property in your Trainee class (I will just call it selectedProperty for demonstration), you can do something like this:
final CheckBoxTableCell<Trainee, Boolean> ctCell = new CheckBoxTableCell<>(); ctCell.setSelectedStateCallback(new Callback<Integer, ObservableValue<Boolean>>() { @Override public ObservableValue<Boolean> call(Integer index) { return table.getItems().get(index).selectedProperty(); } });
Then, the property in the Trainee class must be bidirectionally bound to the state of the flag. If you need to do more than just update the model object when the checkbox is checked or unchecked, you can simply observe this property.
If you do not have a property in the Trainee class, you can simply create a BooleanProperty and watch it:
final CheckBoxTableCell<Trainee, Boolean> ctCell = new CheckBoxTableCell<>(); final BooleanProperty selected = new SimpleBooleanProperty(); ctCell.setSelectedStateCallback(new Callback<Integer, ObservableValue<Boolean>>() { @Override public ObservableValue<Boolean> call(Integer index) { return selected ; } }); selected.addListener(new ChangeListener<Boolean>() { @Override public void changed(ObservableValue<? extends Boolean> obs, Boolean wasSelected, Boolean isSelected) { System.out.println(isSelected); } });
As usual, all this code looks a lot nicer in Java 8.
source share