javafx添加记录后如何刷新表

时间:2018-11-06 16:10:05

标签: javafx

我看到了很多答案,但它不适合我,因为我正在使用循环生成我的表,而不是特定的类 我想刷新从循环生成数据的表。

我尝试刷新表格,但现在可以工作了 我试图再次建立日期,但它重复了表

在这里是循环生成的单元格

            while(rs.next()){
                //Iterate Row
                 ObservableList<String> row = FXCollections.observableArrayList();
                for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
                    //Iterate Column
                    row.add(rs.getString(i));
                }
                System.out.println("Row [1] added "+row );
                data.add(row);
                table.setItems(data);

            }    

这是添加按钮

        buttonAdd.setOnAction(new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent t){
            try {
                stmt.executeUpdate("INSERT into stock VALUES('"
                + fieldCategoryID.getText()+ "','" + fieldCategoryName.getText() + "','" + fieldAmount.getText() + "','" + fieldCostPrice.getText()+ "','" + fieldSellingPrice.getText()
                        + "','" + fieldCategoryProfit.getText()+ "','" + fieldSupplier.getText()+ "','" + fieldCategoryProfitRate.getText()+
                        "','" + fieldTotalCategoryProfit.getText()+"','" + fieldOnStock.getText()+ "')");

        content.getChildren().removeAll(table);
        table.refresh();
        content.getChildren().add(table);
        } catch (SQLException ex) {
                Logger.getLogger(Stock.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        });

这是删除按钮

  buttonDelete.setOnAction(new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent t){
            try {
                                Alert alertConfirmation = new 
table.getSelectionModel().getSelectedIndex();

                TablePosition pos = (TablePosition) table.getSelectionModel().getSelectedCells().get(0);
                            int row = pos.getRow();
                            TableColumn col = pos.getTableColumn();
                             String data = (String) col.getCellObservableValue(table.getItems().get(row)).getValue();
                String query = "DELETE FROM stock where category_code = ?";
                pst = con.prepareStatement(query); 
                pst.setString(1, data);
                pst.execute(); 
}

1 个答案:

答案 0 :(得分:0)

我添加了一种清除observableList数据的刷新方法,然后再次声明了Statement和ResultSet,然后再次添加了行

public void refreshTable() throws SQLException{
data.clear();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
      rs = stmt.executeQuery("select * from stock");
      while(rs.next()){
                //Iterate Row
                  row = FXCollections.observableArrayList();
                for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
                    //Iterate Column
                    row.add(rs.getString(i));
                }
                System.out.println("Row [1] added "+row );
                data.add(row);
                table.setItems(data);

            }   
}