I have defined TableColumn with checkbox type like below in Javafx.
TableColumn<MyObject, Boolean> cbCol = new TableColumn<>(strColName);
cbCol.setCellFactory(CheckBoxCellFactory.forTableColumn(cbCol));
Now I need to trigger onclick event(to perform some operation) on any of the checkbox clicked inside TableColumn. Is there any way to accomplish this ?
Any help is greatly appriciated.
I am using the below custome cell factory to get checkboxes
in the tableCell
. You can use the same and have the onClick
listener here as well. Below is the code:
final class BooleanCell extends TableCell<MyObject, Boolean> {
private CheckBox checkBox;
public BooleanCell() {
checkBox = new CheckBox();
checkBox.setOnAction((evt) -> {
getTableView().getItems().get(getIndex()).setCheck(new SimpleBooleanProperty(checkBox.isSelected()));
}
});
this.setGraphic(checkBox);
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
this.setEditable(true);
}
@Override
public void startEdit() {
super.startEdit();
if (isEmpty()) {
return;
}
checkBox.requestFocus();
}
@Override
public void cancelEdit() {
super.cancelEdit();
// checkBox.setDisable(true);
}
@Override
public void commitEdit(Boolean value) {
super.commitEdit(value);
// checkBox.setDisable(true);
}
@Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
if (item != null) {
checkBox.setAlignment(Pos.CENTER);
checkBox.setSelected(item);
}
setAlignment(Pos.CENTER);
setGraphic(checkBox);
}
}
}
The above cell factory
can be applied to the tableColumn
as mentioned below.
Callback<TableColumn<MyObject, Boolean>, TableCell<MyObject, Boolean>> booleanCellFactory = new Callback<TableColumn<MyObject, Boolean>, TableCell<MyObject, Boolean>>() {
@Override
public TableCell<MyObject, Boolean> call(TableColumn<MyObject, Boolean> p) {
return new BooleanCell();
}
};
tbColCheck.setCellFactory(booleanCellFactory);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.