使表列可用于浮点数据类型

时间:2015-12-14 16:20:55

标签: java mysql hibernate tableview tablecolumn

我成功地使表列可编辑为引用数据库表的string数据类型列的那些列。但是我对数据库表的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()));
        }
    });

如何使第二个代码有效? 谢谢

1 个答案:

答案 0 :(得分:2)

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

我引用了https://stackoverflow.com/a/27915420/5675550,其中展示了如何使用int数据类型编辑表列。我修改了代码并使其适用于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());
    }
}