使用javafx表视图中的对话框将值设置为特定单元格
我成功地使表格列可编辑,并在双击单元格时更新其值。现在我要做的是从txt字段中获取值,并将该值设置为所选行的特定单元格(列)。我做了很多研究,但没有找到一个正确的答案。Javafx不允许直接编辑表中的值,除非直接编辑单元格并设置其值 多谢各位 这是迄今为止我所做工作的一个样本 将cellValueFactory设置为表列使用javafx表视图中的对话框将值设置为特定单元格,java,javafx,tableview,Java,Javafx,Tableview,我成功地使表格列可编辑,并在双击单元格时更新其值。现在我要做的是从txt字段中获取值,并将该值设置为所选行的特定单元格(列)。我做了很多研究,但没有找到一个正确的答案。Javafx不允许直接编辑表中的值,除非直接编辑单元格并设置其值 多谢各位 这是迄今为止我所做工作的一个样本 将cellValueFactory设置为表列 tblColQuantity.setCellValueFactory(cellData -> cellData.getValue() .quantityP
tblColQuantity.setCellValueFactory(cellData -> cellData.getValue()
.quantityProperty());
tblColQuantity.setCellFactory(col -> new IntegerEditingCell());
tblColRateWithoutvat.setCellValueFactory(cellData -> cellData
.getValue().rateWithoutvatProperty());
tblColRateWithoutvat.setCellFactory(col -> new IntegerEditingCell());
tblColTotalWithvat.setCellValueFactory(cellData -> cellData.getValue().totalWithvatProperty());
tblColTotalWithvat.setCellFactory(col -> new IntegerEditingCell());
帮助我更新单元格数据的内部类
public class IntegerEditingCell extends TableCell<AddBillTable, Number> {
private final TextField textField = new TextField();
private final Pattern intPattern = Pattern.compile("\\d*\\.\\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 || value.equals(null)) {
setText(null);
setGraphic(null);
} else if (isEditing()) {
setText(null);
textField.setText(value.toString());
setGraphic(textField);
}/*
* else if (!empty){ textField.setText(value.toString()); }
*/else {
// if((!value.toString().equals(null)) || (value==null)){
setText(value.toString());
setGraphic(null);
System.out.println("Updated");
System.out.println(this.textField.getText());
// }
}
}
@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);
// ((PurchaseDetail)this.getTableRow().getItem()).setQuantity(value.floatValue());
System.out.println("Commit edit " + value);
detectEditedCell(value);
}
}
公共类IntegerEditingCell扩展了TableCell{
私有最终文本字段TextField=新文本字段();
私有最终模式intPattern=Pattern.compile(\\d*\.\\d+);
//-?\\d+
公共整数编辑单元(){
textField.focusedProperty().addListener(
(obs、wasFocused、isNowFocused)->{
如果(!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());
设置图形(文本字段);
}/*
*如果(!empty){textField.setText(value.toString());}
*/否则{
//如果((!value.toString().equals(null))| |(value==null)){
setText(value.toString());
设置图形(空);
系统输出打印项次(“更新”);
System.out.println(this.textField.getText());
// }
}
}
@凌驾
公开作废已启动IT(){
super.startEdit();
数值=getItem();
if(值!=null){
textField.setText(value.toString());
设置图形(文本字段);
setText(空);
}
}
@凌驾
公共作废取消编辑(){
super.cancelEdit();
setText(getItem().toString());
设置图形(空);
}
//这似乎有必要在失去焦点时继续编辑;不确定
//原因:
@凌驾
公共作废提交(数值){
超级。承诺(价值);
//((PurchaseDetail)this.getTableRow().getItem()).setQuantity(value.floatValue());
System.out.println(“提交编辑”+值);
检测到的itedCell(值);
}
}
您只需从表中获取所选项,并调用与该列表示的属性相对应的set方法即可
例如,如果希望文本字段更新当前选定行的数量
,则可以执行以下操作:
TextField textField = new TextField();
textField.setOnAction(e -> {
AddBillTable selectedItem = table.getSelectionModel().getSelectedItem();
if (selectedItem != null) {
selectedItem.setQuantity(Integer.parseInt(textField.getText()));
}
});
只要您使用JavaFX可观察属性(
StringProperty
,IntegerProperty
等)实现模型类(AddBillTable
),然后更改属性值将自动更新表。在不知道如何设置表的情况下回答您的问题并不容易(特别是,哪些类表示每行中的数据,哪些属性映射到您感兴趣的列)。如果你发布相关代码,你会得到一个更好的答案。那么,你想从文本字段更新哪一列呢?它工作得很好。我创造了一个