Java – Checkbox cell factory + Tableview on JavaFX

checkboxjavajavafxtableview

I'm writing a JavaFX client for my soap service, and of my fxml pages must contain a fully-editable TableView, which consists of Product-class entities.My table consists now of 2 text columns and one, which consists of Double values.I want to add a selection column with CheckBox items in it cells.Using a Ensemble demo app I extended a Cell class for using a CheckBoxes :

public class CheckBoxCell<S, T> extends TableCell<S, T> {

private final CheckBox checkBox;
private ObservableValue<T> ov;

public CheckBoxCell() {
    this.checkBox = new CheckBox();
    this.checkBox.setAlignment(Pos.CENTER);
    setAlignment(Pos.CENTER);
    setGraphic(checkBox);
}

@Override
public void updateItem(T item, boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
        setText(null);
        setGraphic(null);
    } else {
        setGraphic(checkBox);
        if (ov instanceof BooleanProperty) {
            checkBox.selectedProperty().unbindBidirectional((BooleanProperty) ov);
        }
        ov = getTableColumn().getCellObservableValue(getIndex());
        if (ov instanceof BooleanProperty) {
            checkBox.selectedProperty().bindBidirectional((BooleanProperty) ov);
        }
    }
}

@Override
public void startEdit() {
    super.startEdit();
    if (isEmpty()) {
        return;
    }
    checkBox.setDisable(false);
    checkBox.requestFocus();
}

@Override
public void cancelEdit() {
    super.cancelEdit();
    checkBox.setDisable(true);
}
}

Then in fxml view controller class I set a cellFactory for requed TableColumn :

private Callback<TableColumn, TableCell> createCheckBoxCellFactory() {
    Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>  () {
        @Override
        public TableCell call(TableColumn p) {
            return new CheckBoxCell();
        }
    };
    return cellFactory;
}

...
products_table_remove.setCellFactory(createCheckBoxCellFactory());

My question is :

1) how to fill this column with unchecked CheckBoxes using PropertyValueFactory if i have

private final ObservableList <Boolean> productsToRemove= FXCollections.observableArrayList();

consists of Boolean.FALSE values then view is created. (TableView consists of Product class that does'nt have a Boolean property (only 3 String and one Double property)) ?

2) Can i get acess to Product object, which contain selected row using EventHandler :

private void setProductDescColumnCellHandler() {
    products_table_remove.setOnEditCommit(new EventHandler() {
        @Override
        public void handle(CellEditEvent t) {
        ...

I saw a lot of examples with Entites, which have a Boolean field.In my case, i dont want to add boolean field to jax-ws generated classes.

Best Answer

1) Predefined class javafx.scene.control.cell.CheckBoxTableCell may be used in place of yours.

2) To add information to an existing instance, I suggest inheritance + delegation, for each data instance, instantiate a view instance which may be used to feed the TableView :

class ProductV extends Product {

   ProductV( Product product ) {
      this.product = product;
   }

   final Product         product;
   final BooleanProperty delected = new SimpleBooleanProperty( false );
}