如何使用可观察列表和 editable table 视图更新我的数据

How can I update my data using an observable list and an editable table view

我自己已经尝试了很长一段时间并进行了大量研究。但是,我还没有想出解决办法。 在我的 table 视图中,我有一个 editable 列。我可以更改 table 中的数据并在控制器中打印它。

表格视图:

    <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());
    }

数据在sqlite数据库中,用可观察列表表示

数据Class:

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;
    }
}

数据访问Class:

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

控制器有这个代码片段来填充table视图:

    public void initialize(){
        
        dataAccess = new SupplyDataAccess();
        dataAccess.loadData();
        tblSupplies.setItems(dataAccess.getSupplies());
    }

行得通,我可以更改数量。它在 table 视图中可见。如何将新值写回可观察列表并从那里写回数据库?

如何访问控制器中 changeQty 的其他字段? Part 和 Location 是我需要在底层数据库中识别记录的字段。如果我可以在控制器中访问这些值,我可以将它们传递给数据访问 class 并创建一个 SQL 语句。这不是问题。

提前致谢,

迈克尔

我想通了。
我创建的提交处理程序 (onEditCommit="#changeQty") 除了显示新值外什么也没做。在我删除提交处理程序后,ObservableList 已按预期更新。
这让我回到了如何更新数据库以使更改持久化的问题上。
我再次创建提交处理程序,更新可观察列表,并在数据访问 class 中调用一个方法来更新数据库。像这样

    @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 为我指明了正确的方向。 也许这对以后的其他人有帮助。