Javafx 如何使TableCell可编辑,以便自动更新数据类?
我正在为一个学校项目制作一个系统,其中一部分是一个TableView,它使用我自己的数据类InventoryData填充行,InventoryData具有与表列对应的属性。我希望使用TextField使某些列中的单元格可编辑,以便在提交编辑时更新InventoryData对象的相关属性 我尝试将TextFieldTableCell.forTableColumn设置为列的单元格工厂。虽然现在提交编辑后,单元格中的文本将更改,但我认为它不会更改InventoryData对象中的属性。我之所以这样认为,是因为当我在编辑一次后再次尝试编辑该单元格时,TextField会在第一次编辑之前显示前一个值 是我做错了什么,还是这是正常的行为,我必须自己实现承诺 以下是InventoryData的代码: 您需要您的InventoryData类才能使用。具体来说,它需要属性类型访问器方法来检索表单元格中的属性。否则,单元格值工厂只调用标准的getName或getId方法,并将结果包装在ReadOnlyStringWrapper或ReadOnlyIntegraterRapper中:表单元格无法更改这些包装的值,因为它们是只读的Javafx 如何使TableCell可编辑,以便自动更新数据类?,javafx,edit,tablecell,Javafx,Edit,Tablecell,我正在为一个学校项目制作一个系统,其中一部分是一个TableView,它使用我自己的数据类InventoryData填充行,InventoryData具有与表列对应的属性。我希望使用TextField使某些列中的单元格可编辑,以便在提交编辑时更新InventoryData对象的相关属性 我尝试将TextFieldTableCell.forTableColumn设置为列的单元格工厂。虽然现在提交编辑后,单元格中的文本将更改,但我认为它不会更改InventoryData对象中的属性。我之所以这样认为
public class InventoryData {
// From Product
private Product productObj;
private IntegerProperty id;
private StringProperty name;
// Constructor - converts Product obj into InventoryData
public InventoryData(Product product)
{
this.productObj = product;
this.id = new SimpleIntegerProperty(product.getId());
this.name = new SimpleStringProperty(product.getName())
this.name.addListener((obs, oldName, newName) ->
productObj.setName(newName));
}
// GET & SET
public Product getProduct()
{
return productObj;
}
public IntegerProperty idProperty() {
return id ;
}
public final int getId() {
return idProperty().get();
}
public final void setId(int id) {
idProperty().set(id);
}
public StringProperty nameProperty() {
return name ;
}
public final String getName() {
return nameProperty().get();
}
public final void setName(String name) {
this.nameProperty().set(name);
// productObj.setName(name);
// System.out.println(productObj.getName());
}
}
我没想到,谢谢你,这似乎管用。能否指定在提交表单元格时调用什么?因为如果有可能的话,如果它也能在这个过程中完成productObj.setNamename,那就太好了。编辑:我根本没有注意到构造函数中的侦听器--它调用nameProperty.set…,因此侦听器应该保持product.name同步…是的,正如我所说,我的眼睛有点跳过了侦听器的行:谢谢你的帮助!祝你今天愉快
public class InventoryData {
// From Product
private Product productObj;
private IntegerProperty id;
private StringProperty name;
// Constructor - converts Product obj into InventoryData
public InventoryData(Product product)
{
this.productObj = product;
this.id = new SimpleIntegerProperty(product.getId());
this.name = new SimpleStringProperty(product.getName())
this.name.addListener((obs, oldName, newName) ->
productObj.setName(newName));
}
// GET & SET
public Product getProduct()
{
return productObj;
}
public IntegerProperty idProperty() {
return id ;
}
public final int getId() {
return idProperty().get();
}
public final void setId(int id) {
idProperty().set(id);
}
public StringProperty nameProperty() {
return name ;
}
public final String getName() {
return nameProperty().get();
}
public final void setName(String name) {
this.nameProperty().set(name);
// productObj.setName(name);
// System.out.println(productObj.getName());
}
}