setup JavaFX Table Editable Boolean Column - Java JavaFX

Java examples for JavaFX:Table

Description

setup JavaFX Table Editable Boolean Column

Demo Code

/*/* w w  w. j ava 2 s.c om*/
 * Copyright 2002-2014 SCOOP Software GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import javafx.beans.property.Property;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.control.OverrunStyle;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.Callback;
import javafx.util.StringConverter;

public class Main{
    public static <T> void setupEditableBooleanColumn(
            TableColumn<T, Boolean> column,
            final ColumnBooleanAccessor<T> propertyAccessor) {
        column.getTableView().setEditable(true);
        column.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<T, Boolean>, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(
                    CellDataFeatures<T, Boolean> param) {
                return propertyAccessor.getProperty(param.getValue());
            }
        });
        column.setOnEditCommit(new EventHandler<CellEditEvent<T, Boolean>>() {
            @Override
            public void handle(CellEditEvent<T, Boolean> t) {
                propertyAccessor.getProperty(t.getRowValue()).setValue(
                        t.getNewValue());
            }
        });
        column.setCellFactory(CheckBoxTableCell.forTableColumn(column));
        column.setEditable(true);
    }
}

Related Tutorials