核心代码
TableColumn<Person, CheckBox> registered_col = new TableColumn<Person, CheckBox>("Registered");
// registered_col.setCellValueFactory(new PropertyValueFactory("checkBox"));
registered_col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person,CheckBox>, ObservableValue<CheckBox>>() {
@Override
public ObservableValue<CheckBox> call(CellDataFeatures<Person, CheckBox> param) {
CheckBox checkBox = new CheckBox();
checkBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("nihao ");
}
});
return new ReadOnlyObjectWrapper<CheckBox>(checkBox);
}
});
// registered_col.getCellData(index)
registered_col.setCellFactory(new Callback<TableColumn<Person,CheckBox>, TableCell<Person,CheckBox>>() {
@Override
public TableCell<Person, CheckBox> call(TableColumn<Person, CheckBox> param) {
TableCell<Person, CheckBox> tableCell = new TableCell<Person, CheckBox>(){
@Override
protected void updateItem(CheckBox item, boolean empty) {
super.updateItem(item, empty);
if(!empty)
setGraphic(item);
}
};
return tableCell;
}
});
//====================如果需要数据同步,即数据由多变少时的同步
TableColumn<OrderBean, CheckBox> is_selectCol = new TableColumn<OrderBean, CheckBox>("选择");
is_selectCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<OrderBean,CheckBox>, ObservableValue<CheckBox>>() {
@Override
public ObservableValue<CheckBox> call(CellDataFeatures<OrderBean, CheckBox> param) {
CheckBox is_select = param.getValue().is_selectProperty();
return new ReadOnlyObjectWrapper<CheckBox>(is_select);
}
});
is_selectCol.setCellFactory(new Callback<TableColumn<OrderBean,CheckBox>, TableCell<OrderBean,CheckBox>>() {
@Override
public TableCell<OrderBean, CheckBox> call(TableColumn<OrderBean, CheckBox> param) {
TableCell<OrderBean, CheckBox> tableCell = new TableCell<OrderBean, CheckBox>(){
@Override
protected void updateItem(CheckBox item, boolean empty) {
super.updateItem(item, empty);
if(empty){
setGraphic(null);
}else {
setGraphic(item);
}
}
};
return tableCell;
}
});