TableView,设置可编辑单元格

TableView, setting editable cells

我尝试使 Table 个单元格可编辑。我设法用两个包含字符串值的列来做到这一点,但我不能用代表整数值的列来做到这一点。

带有 X 的地方是编译器出错的地方:

The method setCellFactory(Callback<TableColumn<DataModel,Integer>,TableCell<DataModel,Integer>>) in the type TableColumn<DataModel,Integer> is not applicable for the arguments  (Callback<TableColumn<DataModel,String>,TableCell<DataModel,String>>)

带有 XX 的地方是编译器得到错误的地方:

The method setOnEditCommit(EventHandler<TableColumn.CellEditEvent<DataModel,Integer>>) in the type TableColumn<DataModel,Integer> is not applicable for the arguments ((CellEditEvent<DataModel, Integer> event) -> {})

代码如下:

public void initialize(URL location, ResourceBundle resources) {
    //Tworzymy sobie kolumny, które będą odpowiadać oraz przyjmować konretne dane
    TableColumn<DataModel, String> nameColumn = new TableColumn<DataModel, String>("Name");
    nameColumn.setMinWidth(100);
    TableColumn<DataModel, String> surnameColumn = new TableColumn<DataModel, String>("Surname");
    surnameColumn.setMinWidth(100);
    TableColumn<DataModel, Integer> ageColumn = new TableColumn<DataModel, Integer>("Age");
    ageColumn.setMinWidth(100);
    TableColumn<DataModel, Integer> telNumberColumn = new TableColumn<DataModel, Integer>("Tel. Number");
    telNumberColumn.setMinWidth(100);

    //dodajemy kolumny do okna
    tableView.getColumns().addAll(nameColumn,surnameColumn,ageColumn,telNumberColumn);

    //podajemy nazwy zmiennych, których wartości mają się wyświetlać w poszczególnych kolumnach
    nameColumn.setCellValueFactory(new PropertyValueFactory<>("sName"));
    surnameColumn.setCellValueFactory(new PropertyValueFactory<>("sSurname"));
    ageColumn.setCellValueFactory(new PropertyValueFactory<>("iAge"));
    telNumberColumn.setCellValueFactory(new PropertyValueFactory<>("iPhoneNumber"));

    //Sprawiamy że poszczególne kolumny stają się edytowalne
    nameColumn.setCellFactory(TextFieldTableCell.<DataModel>forTableColumn());
    nameColumn.setOnEditCommit((CellEditEvent<DataModel, String> event) -> {
        ((DataModel) event.getTableView().getItems(). get(event.getTablePosition().getRow())).  setsName(event.getNewValue());
    });
    surnameColumn.setCellFactory(TextFieldTableCell.<DataModel>forTableColumn());
    surnameColumn.setOnEditCommit((CellEditEvent<DataModel, String> event) -> {
        ((DataModel) event.getTableView().getItems(). get(event.getTablePosition().getRow())).  setsSurname(event.getNewValue());
    });
X   ageColumn.setCellFactory(TextFieldTableCell.<DataModel>forTableColumn());
XX  ageColumn.setOnEditCommit((CellEditEvent<DataModel, Integer> event) -> {
    //    ((DataModel) event.getTableView().getItems(). get(event.getTablePosition().getRow())).  setiAge(Integer.valueOf(event.getNewValue()));

    });
X   telNumberColumn.setCellFactory(TextFieldTableCell.<DataModel>forTableColumn());
XX  telNumberColumn.setOnEditCommit((CellEditEvent<DataModel, Integer> event) -> {
 //         ((DataModel) event.getTableView().getItems(). get(event.getTablePosition().getRow())).  setiPhoneNumber(Integer.valueOf(event.getNewValue()));

    });

    tableView.setPlaceholder(new Label("Pust tabelka!"));//jaki element dodać jeśli tabelka nie jest wyświetlona
    tableView.setEditable(true);

    tableView.setItems(dataList); //wczytujemy dane do przygotowanej tabelki

    buttAdd.setOnAction((ActionEvent e) -> {
        buttAddAction(e);
    });
}

我正在学oracle Table查看教程,难度比较大。帮助。

问题是 TextFieldTableCell.forTableColumn() 被键入了一个字符串值。查看默认实现:

public static <S> Callback<TableColumn<S,String>, TableCell<S,String>> forTableColumn() {
    return forTableColumn(new DefaultStringConverter());
}

你需要的是 TextFieldTableCell 加上 IntegerStringConverter,例如:

ageColumn.setCellFactory(TextFieldTableCell.<DataModel, Integer>forTableColumn(new IntegerStringConverter()));

我搜索了很多答案,并且 borrowed/extended/merged 找到了这个解决方案。当焦点从已编辑的单元格移开时,将提交编辑。对于可以在 table 中表示的每种数据类型,我都有一个 public class:EditingTextCell、EditingIntegerCell 等。这些 public classes 可以应用于任何table 前提是数据表示为 class 的可观察列表,该列表访问要显示为属性的数据。我发布此解决方案是因为我面临着为我的应用程序中每个 table 的每一列创建一个 class。目前,双精度值和组合框单元格版本绑定到特定 table 的特定列。如果时间允许,我会做这些的通用版本。请原谅我没有提供源链接——我在仔细阅读它们时忘记将它们添加为书签。

Java 文档表明即将推出更简单的方法。

整数字段的用法示例:

    TableColumn<Factor, Number> noLevelsCol =
            new TableColumn<>("No. Levels");
    noLevelsCol.setCellValueFactory(
            new PropertyValueFactory("numberLevels"));
    noLevelsCol.setMinWidth(40);
    noLevelsCol.setCellFactory(col -> new EditingIntegerCell<>());
    noLevelsCol.setOnEditCommit((CellEditEvent<Factor, Number> t) -> {
        ((Factor) t.getTableView().getItems().get(
                t.getTablePosition().getRow())
                ).setNumberLevels(t.getNewValue().intValue());
    });

字符串字段的用法示例:

    TableColumn<Factor, String> nameCol = new TableColumn<>("Name");
    nameCol.setMinWidth(60);
    nameCol.setCellValueFactory(
          new PropertyValueFactory("factorName"));
    nameCol.setCellFactory(cellFactory);
    nameCol.setOnEditCommit((CellEditEvent<Factor, String> t) -> {
        ((Factor) t.getTableView().getItems().get(
                t.getTablePosition().getRow())
                ).setFactorName(t.getNewValue());
    });

因子定义class: public class 因子 {

private final IntegerProperty factorID = new SimpleIntegerProperty();
public IntegerProperty getFactorID() {     return factorID;    }

private StringProperty factorName = new SimpleStringProperty();
public void setFactorName(String value) {
           factorNameProperty().set(value); }
public String getFactorName() { return factorNameProperty().get(); }
public StringProperty factorNameProperty() { 
    if (factorName == null) factorName = 
        new SimpleStringProperty(this, "factorName");
    return factorName; 
}

private IntegerProperty numberLevels = new SimpleIntegerProperty();
public void setNumberLevels(int value) {
         numberLevelsProperty().set(value); }
public IntegerProperty getNumberLevels() { return numberLevels;    }
public IntegerProperty numberLevelsProperty() { 
    if (numberLevels == null) numberLevels =
          new SimpleIntegerProperty(this, "numberLevels");
    return numberLevels; 
}

private StringProperty listOfLevels = new SimpleStringProperty();
public void setListOfLevels(String value) {
         listOfLevelsProperty().set(value); }
public String getListOfLevels() { return listOfLevelsProperty().get(); }
public StringProperty listOfLevelsProperty() { 
    if (listOfLevels == null) listOfLevels = 
          new SimpleStringProperty(this, "listOfLevels");
    return listOfLevels; 
}


// Constructors
public Factor(int factorID, String factorName) {
    this.factorID.set(factorID);
    this.factorName.set(factorName);
    this.numberLevels.set(1);
    this.listOfLevels.set("-1, 1");
}

public Factor(int factorID, String factorName, int numberLevels,
                String listOfLevels) {
    this.factorID.set(factorID);
    this.factorName.set(factorName);
    this.numberLevels.set(numberLevels);
    this.listOfLevels.set(listOfLevels);
}

@Override
public String toString() {
    return "Factor{" + "factorName=" + factorName + '}';
}

public String[] getLevels() {
    return listOfLevels.getValue().split(",");
}

}

正在将数据加载到 table 最终 ObservableList factorList = FXCollections.observableArrayList( 新因素(1,"Factor1",2,“-1,1”) );

    factorTableView.setEditable(true);
    factorTableView.getColumns().clear();
    factorTableView.setItems(factorList);
    boolean addAll;
    addAll = factorTableView.getColumns().addAll(idCol,
                   nameCol, noLevelsCol, levelsCol);

EditingIntegerCell class public class EditingIntegerCell 扩展了 TableCell {

    private TextField textField;
    private final Pattern intPattern = Pattern.compile("-?\d+");

    public EditingIntegerCell() {
    }

    @Override
    public void startEdit() {
        if (!isEmpty()) {
            super.startEdit();
            createTextField();
            setText(null);
            setGraphic(textField);
            textField.selectAll();
        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();

        setText((String) getItem().toString());
        setGraphic(null);
    }

    @Override
    public void updateItem(Number item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());
                }
                setText(null);
                setGraphic(textField);
            } else {
                setText(getString());
                setGraphic(null);
            }
        }
    }

    private void createTextField() {
        textField = new TextField(getString());
        textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
        textField.focusedProperty().addListener(
            (ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) 
                        -> {
            if (!arg2) {
                processEdit();
            }
        });
    }

    private void processEdit() {
        String text = textField.getText();
        if (intPattern.matcher(text).matches()) {
            commitEdit(Integer.parseInt(text));
        } else {
            cancelEdit();
        }
    }

    private String getString() {
        return getItem() == null ? "" : getItem().toString();
    }
}

** EditingTextCell class ** public class EditingTextCell 扩展了 TableCell {

    private TextField textField;

    public EditingTextCell() {
    }

    @Override
    public void startEdit() {
        if (!isEmpty()) {
            super.startEdit();
            createTextField();
            setText(null);
            setGraphic(textField);
            textField.selectAll();
        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();

        setText((String) getItem());
        setGraphic(null);
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());
                }
                setText(null);
                setGraphic(textField);
            } else {
                setText(getString());
                setGraphic(null);
            }
        }
    }

    private void createTextField() {
        textField = new TextField(getString());
        textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
        textField.focusedProperty().addListener(
            (ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) 
                        -> {
            if (!arg2) {
                commitEdit(textField.getText());
            }
        });
    }

    private String getString() {
        return getItem() == null ? "" : getItem();
    }
}