javacheckboxtableviewtablecelljavafx-2

How to add CheckBox's to a TableView in JavaFX


In my Java Desktop Application I have a TableView in which I want to have a column with CheckBoxes.

I did find where this has been done on Jonathan Giles's website http://www.jonathangiles.net (the page where this was published in 2011 is no longer active so I removed the old link from the post), However, the download is not available and Jonathan Giles did not answer my email, so I thought I'd ask...

How do I put a CheckBox in a cell of my TableView?


Solution

  • You need to set a CellFactory on the TableColumn.

    For example:

    Callback<TableColumn<TableData, Boolean>, TableCell<TableData, Boolean>> booleanCellFactory = 
                new Callback<TableColumn<TableData, Boolean>, TableCell<TableData, Boolean>>() {
                @Override
                    public TableCell<TableData, Boolean> call(TableColumn<TableData, Boolean> p) {
                        return new BooleanCell();
                }
            };
            active.setCellValueFactory(new PropertyValueFactory<TableData,Boolean>("active"));
            active.setCellFactory(booleanCellFactory);
    
    class BooleanCell extends TableCell<TableData, Boolean> {
            private CheckBox checkBox;
            public BooleanCell() {
                checkBox = new CheckBox();
                checkBox.setDisable(true);
                checkBox.selectedProperty().addListener(new ChangeListener<Boolean> () {
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                        if(isEditing())
                            commitEdit(newValue == null ? false : newValue);
                    }
                });
                this.setGraphic(checkBox);
                this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                this.setEditable(true);
            }
            @Override
            public void startEdit() {
                super.startEdit();
                if (isEmpty()) {
                    return;
                }
                checkBox.setDisable(false);
                checkBox.requestFocus();
            }
            @Override
            public void cancelEdit() {
                super.cancelEdit();
                checkBox.setDisable(true);
            }
            public void commitEdit(Boolean value) {
                super.commitEdit(value);
                checkBox.setDisable(true);
            }
            @Override
            public void updateItem(Boolean item, boolean empty) {
                super.updateItem(item, empty);
                if (!isEmpty()) {
                    checkBox.setSelected(item);
                }
            }
        }