setup JavaFX Table Editable String Column - Java JavaFX

Java examples for JavaFX:Table

Description

setup JavaFX Table Editable String Column

Demo Code

/*/*from www  .j  ava  2s. co m*/
 * 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{
    /**
     * T Tableobject
     * Column Display Type
     */
    public static <T> void setupEditableStringColumn(
            TableColumn<T, String> column,
            final ColumnStringAccessor<T> propertyAccessor) {
        column.getTableView().setEditable(true);
        column.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<T, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(
                    CellDataFeatures<T, String> param) {
                return propertyAccessor.getProperty(param.getValue());
            }
        });
        column.setEditable(true);
        column.setOnEditCommit(new EventHandler<CellEditEvent<T, String>>() {
            @Override
            public void handle(CellEditEvent<T, String> t) {
                propertyAccessor.getProperty(t.getRowValue()).setValue(
                        t.getNewValue());
            }
        });
        column.setCellFactory(new Callback<TableColumn<T, String>, TableCell<T, String>>() {
            @Override
            public TableCell<T, String> call(TableColumn<T, String> param) {
                return new EditingCell<T>();
            }
        });
    }
}

Related Tutorials