问题描述
我自己已经尝试了很长一段时间并进行了大量研究。但是,我还没有想出解决办法。 在我的表格视图中,我有一个可编辑的列。我可以更改该表中的数据并将其打印在控制器中。
表视图:
col.replace_one({'store_id': i['store_id']},i,upsert=True)
控制器:
<TableColumn fx:id="colQTY" editable="true" onEditCommit="#changeQty" prefWidth="35.0" text="Ist">
<cellValueFactory>
<PropertyValueFactory property="sQuantity" />
</cellValueFactory>
<cellFactory>
<TextFieldTableCell fx:factory="forTableColumn" />
</cellFactory>
</TableColumn>
数据类:
@FXML
private void changeQty(CellEditEvent<?,?> event) {
System.out.println(event.getNewValue().toString());
}
数据访问类:
public class SupplyData {
public SimpleStringProperty sPart;
public SimpleStringProperty sLocation;
public SimpleStringProperty sQuantity;
public SupplyData(String sPart,String sLocation,String sQuantity) {
this.sPart= new SimpleStringProperty(sPart);
this.sLocation= new SimpleStringProperty(sLocation);
this.sQuantity= new SimpleStringProperty(sQuantity);
}
public void setsPart(String sPart) {
this.sPart.set(sPart);
}
public String getsPartl() {
return sPart.get();
}
public SimpleStringProperty sPartproperty() {
return sPart;
}
public void setsLocation(String sLocation) {
this.sLocation.set(sLocation);
}
public String getsLocation() {
return sLocation.get();
}
public SimpleStringProperty sLocationproperty() {
return sLocation;
}
public void setsQuantity(String sQuantity) {
this.sQuantity.set(sQuantity);
}
public String getsQuantity() {
return sQuantity.get();
}
public SimpleStringProperty sQuantityproperty() {
return sQuantity;
}
}
控制器有这个代码片段来填充表格视图:
public class SupplyDataAccess {
private ObservableList<SupplyData> supplies;
public SupplyDataAccess() {
supplies = FXCollections.observableArrayList();
}
public ObservableList<SupplyData> getSupplies() {
return supplies;
}
public static Connection connectDB(String sDB) {
Connection myDB = null;
try {
myDB = DriverManager.getConnection(sDB);
}catch (sqlException e) {
System.out.println(e.getMessage());
}
return myDB;
}
public void loadData(){
SupplyData data;
String sDB = "jdbc:sqlite:c:\\supplies.db";
String sQuery = "select * from supplies;";
Connection myDB = connectDB(sDB);
try{
ResultSet rs = myDB.createStatement().executeQuery(sQuery);
while(rs.next()) {
data = new SupplyData( rs.getString("Part"),rs.getString("Location"),rs.getString("Quantity"));
supplies.add(data);
} //while
rs.close();
myDB.close();
} catch (sqlException e) {
System.out.println(e.getMessage());
}
}//loadData
} //class
那行得通,我可以更改数量。它在表格视图中可见。如何将新值写回可观察列表并从那里写入数据库?
如何访问控制器中 changeQty 中的其他字段? Part 和 Location 是我需要在底层数据库中识别记录的字段。如果我可以在控制器中访问这些值,我就可以将它们传递给数据访问类并创建一个 sql 语句。这不是问题。
提前致谢,
迈克尔
解决方法
我想通了。
我创建的提交处理程序 (onEditCommit="#changeQty") 除了显示新值之外什么也没做。删除提交处理程序后,ObservableList 已按原样更新。
这让我回到了如何更新数据库以使更改持久化的问题。
我再次创建了提交处理程序,更新了可观察列表,并调用了数据访问类中的一个方法来更新数据库。像这样
@FXML
private void updateQty(CellEditEvent<SupplyData,String> event) {
//this defines ChangedSupply and writes the current row of the table to it
SupplyData ChangedSupply = event.getRowValue();
//this updates the observable list
ChangedSupply.setsQuantity(event.getNewValue());
//this in the end updates the database
dataAccess.updateData(ChangedSupply);
}
感谢@kleopatra 为我指明了正确的方向。 也许这会在未来对其他人有所帮助。