使 Table 列可编辑为浮点数据类型

Making Table Column editable for float data types

我成功地为那些引用数据库 table 的 string 数据类型列的列创建了 table 列 editable。但是我对数据库 table.

float 数据类型列做同样的事情是不成功的
    tblColProductID.setCellValueFactory(new PropertyValueFactory<ProductHeader, String>("Product_ID"));
    tblColProductName.setCellFactory(TextFieldTableCell.forTableColumn());
    tblColProductName.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<ProductHeader,String>>() {
        @Override
        public void handle(CellEditEvent<ProductHeader, String> t) {
            // TODO Auto-generated method stub
            ((ProductHeader) t.getTableView().getItems()
                    .get(t.getTablePosition().getRow())).setProduct_ID((String) t.getNewValue());
        }

上面的tblColProductID指的是具有字符串数据类型的ProductID列。但是下面的代码在 setCellFactory

中给我错误
tblColQuantity.setCellValueFactory(new PropertyValueFactory<PurchaseDetail, Float>("Quantity"));
tblColQuantity.setCellFactory(TextFieldTableCell.forTableColumn());
tblColQuantity.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<PurchaseDetail,Float>>() {
  @Override
  public void handle(CellEditEvent<PurchaseDetail, Float> t) {
    ((PurchaseDetail) t.getTableView().getItems().get(t.getTablePosition().getRow())).setQuantity((t.getNewValue()));
        }
    });

如何使第二个代码起作用? 谢谢

好的,我已经解决了问题,我就是这样做的。

我参考了 ,其中展示了如何使用 int 数据类型编辑 table 列。我修改了代码并使其适用于 float 数据类型。

这是代码:-

tblColQuantity.setCellFactory(col -> new IntegerEditingCell());

public class IntegerEditingCell extends TableCell<ProductHeader, Number> {
    private final TextField textField = new TextField();
    private final Pattern intPattern = Pattern.compile("\d*\.\d+");
    public IntegerEditingCell(){
        textField.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
            if (! isNowFocused) {
                processEdit();
            }
        });
        textField.setOnAction(event -> processEdit());
    }

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

    @Override
    public void updateItem(Number value, boolean empty) {
        super.updateItem(value, empty);
        if (empty) {
            setText(null);
            setGraphic(null);
        }else if (isEditing()) {
            setText(null);
            textField.setText(value.toString());
            setGraphic(textField);
        } else {
            setText(value.toString());
            setGraphic(null);
        }
    }

    @Override
    public void startEdit() {
        super.startEdit();
        Number value = getItem();
        if (value != null) {
            textField.setText(value.toString());
            setGraphic(textField);
            setText(null);
        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();
        setText(getItem().toString());
        setGraphic(null);
    }

    // This seems necessary to persist the edit on loss of focus; not sure why:
    @Override
    public void commitEdit(Number value) {
        super.commitEdit(value);
        ((Item)this.getTableRow().getItem()).setValue(value.floatValue());
    }
}