Java 使浮动数据类型的表列可编辑
对于那些引用数据库表的Java 使浮动数据类型的表列可编辑,java,mysql,hibernate,tableview,tablecolumn,Java,Mysql,Hibernate,Tableview,Tablecolumn,对于那些引用数据库表的字符串数据类型列的列,我成功地使表列可编辑。但我无法对数据库表的float数据类型列执行相同的操作 tblColProductID.setCellValueFactory(new PropertyValueFactory<ProductHeader, String>("Product_ID")); tblColProductName.setCellFactory(TextFieldTableCell.forTableColumn());
字符串
数据类型列的列,我成功地使表列可编辑。但我无法对数据库表的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());
}
如何使第二个代码工作?
谢谢好的,我已经解决了这个问题,我就是这样做的 我引用了一个例子,它展示了如何用
int
datatype编辑一个表列。我修改了代码,使其适用于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());
}
}
tblColQuantity.setCellFactory(col->newintegereditingcell());
公共类IntegerEditingCell扩展了TableCell{
私有最终文本字段TextField=新文本字段();
私有最终模式intPattern=Pattern.compile(\\d*\.\\d+);
公共整数编辑单元(){
textField.focusedProperty().addListener((obs、WASCOURED、ISNOWPOURED)->{
如果(!isNowFocused){
processEdit();
}
});
setOnAction(事件->进程编辑());
}
私有void processEdit(){
String text=textField.getText();
if(intPattern.matcher(text.matches()){
committedit(Float.parseFloat(text));
}否则{
取消编辑();
}
}
@凌驾
public void updateItem(数值,布尔空){
super.updateItem(值,空);
if(空){
setText(空);
设置图形(空);
}else if(isEditing()){
setText(空);
textField.setText(value.toString());
设置图形(文本字段);
}否则{
setText(value.toString());
设置图形(空);
}
}
@凌驾
公开作废已启动IT(){
super.startEdit();
数值=getItem();
if(值!=null){
textField.setText(value.toString());
设置图形(文本字段);
setText(空);
}
}
@凌驾
公共作废取消编辑(){
super.cancelEdit();
setText(getItem().toString());
设置图形(空);
}
//这似乎有必要在失去焦点时继续编辑;不确定原因:
@凌驾
公共作废提交(数值){
超级。承诺(价值);
((项)this.getTableRow().getItem()).setValue(value.floatValue());
}
}
您能在帖子中提供错误堆栈吗?它不会给出任何错误消息。当我将鼠标悬停到错误位置时,会显示类型表列中的方法setCellFactory(回调)不适用于参数(回调)
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());
}
}