dimanche 18 novembre 2018

JavaFx Tableview checkbox requires focus

I implemented boolean representation in my tableView as checkbox. It works fine in general but very irritating fact is that it requires row to be focused (editing) to apply change of checkbox value. It means I first have to double click on the field and then click checkbox.

How to make checkbox change perform onEditCommit right away?

public class BooleanCell<T> extends TableCell<T, Boolean> {
private CheckBox checkBox;

public BooleanCell() {
    checkBox = new CheckBox();
    checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            if (isEditing())
                commitEdit(newValue == null ? false : newValue);
        }
    });
    setAlignment(Pos.CENTER);
    this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    this.setEditable(true);
}

@Override
public void updateItem(Boolean item, boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
        setGraphic(null);
    } else {
        checkBox.setSelected(item);
        setGraphic(checkBox);
    }
}

}




Aucun commentaire:

Enregistrer un commentaire