简体   繁体   English

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

[英]Making Table Column editable for float data types

I am sucessfull in making table column editable for those which refers to a string data type column of database table. 我很成功地使表列可以编辑为那些引用数据库表的string数据类型列的列。 But I am unsucessfull in doing the same with a float data type column of database 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());
        }

The above tblColProductID refers to ProductID column which is has string datatype. 上面的tblColProductID指的是具有字符串数据类型的ProductID列。 But the code below gives me error in setCellFactory 但是下面的代码在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()));
        }
    });

How do I make this second code work? 如何使第二个代码工作? Thank you 谢谢

Okay I have solved the problem and this is how I did it. 好的,我已经解决了问题,这就是我做到的。

I refered to https://stackoverflow.com/a/27915420/5675550 which showed how to edit a table column with int datatype. 我参考https://stackoverflow.com/a/27915420/5675550 ,其中展示了如何使用int数据类型编辑表列。 I modified the code and made it work for float datatypes. 我修改了代码并使其适用于float数据类型。

Here is the code :- 这是代码: -

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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM