简体   繁体   中英

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. But I am unsucessfull in doing the same with a float data type column of database table.

    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. But the code below gives me error in 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. I modified the code and made it work for float datatypes.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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