如何通过TextField向TableView添加多个记录(行)?
到目前为止,我可以设法通过TextField向TableView添加记录,但是当我更改TextField值并点击ADD按钮时,它会删除以前添加的记录并在TableView中显示新记录。
// Code For Inserting Records to TableView Through TextField //
private void Add_details(){
try {
String Customer = txtCustomer.getText().trim();
String Mobile = txtMobile.getText().trim();
String Item = txtItem.getText().trim();
int unit_price = Integer.parseInt(txtUnitPrice.getText().trim());
int qty = Integer.parseInt(txtQty.getText().trim());
TableItems t = new TableItems();
ObservableList <TableItems> curnt_row = FXCollections.observableArrayList();
t.setCustomer(Customer);
t.setMobile(Mobile);
t.setItem(Item);
t.setUnit_price(String.valueOf(unit_price));
t.setQty(String.valueOf(qty));
t.setTotal(String.valueOf(total));
curnt_row.add(t);
tblItems.setItems(curnt_row);
col_customer.setCellValueFactory(new PropertyValueFactory<>("customer"));
col_mobile.setCellValueFactory(new PropertyValueFactory<>("mobile"));
col_item.setCellValueFactory(new PropertyValueFactory<>("item"));
col_qty.setCellValueFactory(new PropertyValueFactory<>("qty"));
col_unitprice.setCellValueFactory(new PropertyValueFactory<>("unit_price"));
col_total.setCellValueFactory(new PropertyValueFactory<>("total"));
}catch (NumberFormatException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
// CORDING FOR GET SELECTED ITEM FROM TABLEVIEW //
// I WANT TO GET ALL ITEMS,
// NOT ONLY SELECTED ITEM SO THAT I COULD PERFORM BATCH INSERTION
private void Get_table_values(){
/* LAMDA EXPRESSION */
tblItems.getSelectionModel().selectedItemProperty().addListener
((obs, oldSelection, newSelection) -> {
if (newSelection != null) {
TableView.TableViewSelectionModel selectionModel = tblItems.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();
TablePosition tablePosition = (TablePosition) selectedCells.get(0);
Object val = tablePosition.getTableColumn().getCellData(newSelection);
String S_value = val.toString();
}
});
}
答案 0 :(得分:0)
您正在替换整个items
列表,而不是简单地添加单个项目。新列表开始为空,您添加的唯一项目是在Add_details
方法中创建的项目。改为添加现有列表中的项目:
private final ObservableList <TableItems> curnt_row = FXCollections.observableArrayList();
...
// TableView initialisation
tblItems.setItems(curnt_row);
col_customer.setCellValueFactory(new PropertyValueFactory<>("customer"));
col_mobile.setCellValueFactory(new PropertyValueFactory<>("mobile"));
col_item.setCellValueFactory(new PropertyValueFactory<>("item"));
col_qty.setCellValueFactory(new PropertyValueFactory<>("qty"));
col_unitprice.setCellValueFactory(new PropertyValueFactory<>("unit_price"));
col_total.setCellValueFactory(new PropertyValueFactory<>("total"));
...
private void Add_details(){
try {
String Customer = txtCustomer.getText().trim();
String Mobile = txtMobile.getText().trim();
String Item = txtItem.getText().trim();
int unit_price = Integer.parseInt(txtUnitPrice.getText().trim());
int qty = Integer.parseInt(txtQty.getText().trim());
TableItems t = new TableItems();
t.setCustomer(Customer);
t.setMobile(Mobile);
t.setItem(Item);
t.setUnit_price(String.valueOf(unit_price));
t.setQty(String.valueOf(qty));
t.setTotal(String.valueOf(total));
curnt_row.add(t);
} catch (Exception e) {
e.printStackTrace();
}
}
要获取所有项目,只需使用getItems
或使用您知道已分配给items
媒体资源的列表,例如在上面的例子中是curnt_row
字段。