gpt4 book ai didi

java - 使 float 据类型的表列可编辑

转载 作者:可可西里 更新时间:2023-11-01 08:27:15 25 4
gpt4 key购买 nike

我成功地使那些引用数据库表的 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()));
}
});

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

最佳答案

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

我引用了 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());
}
}

关于java - 使 float 据类型的表列可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34271801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com