vendredi 21 août 2020

javafx: Toggle multiple CheckBoxTableCell-Checkboxes at once

My Goal

I have an editable table with a boolean column where one can check or uncheck the containing checkboxes and a multiple row selection policy. I would like my table to automatically toggle all checkboxes of all selected rows as soon as i check or uncheck one. Don't know if you get the point :D I mean:

  1. I select multiple rows
  2. I check or uncheck the checkbox of one of those selected rows
  3. All other rows's checkboxes get automatically checked or unchecked

Now it should be clear ;)

My Question

(I'm new to JavaFX! I've already done the same thing I'm asking for with AWT/SWING, but am not able to get it working with JavaFX)

Is there similar already built in JavaFX? If not, what is the best aproch to get to my goal?

What I've done so far

I've found out, that you can listen for a change event by setting a CheckBoxTableCell-Callback to the desired Column's CellFactory. I did it like so:

TableColumn<FileSelection, Boolean> selectedColumn = new TableColumn<>("Sel");
selectedColumn.setCellValueFactory(new PropertyValueFactory<>("selected"));
selectedColumn.setCellFactory(CheckBoxTableCell.forTableColumn(rowidx -> {
    if (tblVideoFiles.getSelectionModel().isSelected(rowidx)) {
        tblVideoFiles.getSelectionModel().getSelectedItems().forEach(item -> {
            if (!item.getFile().equals(tblVideoFiles.getItems().get(rowidx).getFile())) {
                item.selectedProperty().set(!item.selectedProperty().get());
             }
         });
     }
     return fileList.get(rowidx).selectedProperty();
}));

The problem here: As soon as a checkbox gets changed, it toggels itself, resulting in a toggle-loop of checking and unchecking itself :D How can I stop this?




Aucun commentaire:

Enregistrer un commentaire