jeudi 19 mars 2015

JavaFX tri-stated CheckBox in TableView

I want to bind a tri-state CheckBox inside a TableView with a bean property and i have an approximation but i'm sure there is an easy/better way to do it. I looked in google, but i didn't find my answer.



public enum Attending {FALSE, TRUE, UNKNOWN}
colEp.setCellFactory(new Callback<TableColumn<VentanaExtended, Attending>, TableCell<VentanaExtended, Attending>>() {

@Override
public TableCell<VentanaExtended, Attending> call(TableColumn<VentanaExtended, Attending> column) {
return new TableCell<VentanaExtended, Attending>() {
final CheckBox checkBox = new CheckBox() {
{
setAllowIndeterminate(true);
}

};

@Override
public void updateItem(Attending item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
((VentanaExtended) getTableRow().getItem()).attendingProperty().bind(Bindings.when(checkBox.indeterminateProperty()).
then(Attending.UNKNOWN).
otherwise(
Bindings.when(checkBox.selectedProperty()).
then(Attending.TRUE).
otherwise(Attending.FALSE))
);
switch (item) {
case TRUE:
checkBox.setSelected(true);
break;
case FALSE:
checkBox.setSelected(false);
break;
case UNKNOWN:
checkBox.setIndeterminate(true);
break;
}
setGraphic(checkBox);
}
}
;
}
;
}
});




Aucun commentaire:

Enregistrer un commentaire