JavaFX: CheckBoxTableCell get ActionEvent when user check a checkBox

You have two ways of getting a notification when any of the check boxes is clicked.

One: providing a callback as argument for CheckBoxTableCell.forTableColumn instead of the tableColumn:

checkedCol.setCellFactory(CheckBoxTableCell.forTableColumn(new Callback<Integer, ObservableValue<Boolean>>() {

    @Override
    public ObservableValue<Boolean> call(Integer param) {
        System.out.println("Cours "+items.get(param).getCours()+" changed value to " +items.get(param).isChecked());
        return items.get(param).checkedProperty();
    }
}));

Two: providing a callback to the collection:

final List<Cours> items=Arrays.asList(new Cours("Analyse", "3"), 
          new Cours("Analyse TP", "4"), 
          new Cours("Thermo", "5"),
          new Cours("Thermo TP", "7"),
          new Cours("Chimie", "8"));
this.coursData = FXCollections.observableArrayList(new Callback<Cours, Observable[]>() {

    @Override
    public Observable[] call(Cours param) {
        return new Observable[] {param.checkedProperty()};
    }
});
coursData.addAll(items);

and now listening to changes in the collection:

coursData.addListener(new ListChangeListener<Cours>() {

    @Override
    public void onChanged(ListChangeListener.Change<? extends Cours> c) {
        while (c.next()) {
            if (c.wasUpdated()) {
                System.out.println("Cours "+items.get(c.getFrom()).getCours()+" changed value to " +items.get(c.getFrom()).isChecked());
            }
          }
    }
});

Leave a Comment