javafx tableview:添加新条目更改以前的所有数据

时间:2017-01-13 18:15:45

标签: java javafx-8 scenebuilder

每当添加table-view中的新行时,它都会更改表中的所有先前数据,并使所有值都相同。以下图片是一个示例:

following image can explain

在上面的购买发票中,表格中的最后一个添加更改了前一行的值。

相关代码部分是....

public class Pur_InvController implements Initializable {
 Items newItems = new Items(); //instances of bean class Items...   
 ObservableList < Items > list = FXCollections.observableArrayList();

 //...............Declaration of FXML Items...........

 @FXML public ComboBox SupplierBox;
 @FXML public ComboBox ProductBox;
 @FXML public TextField Quantity;
 @FXML public TextField Batch;
 @FXML public TextField Bonus;
 @FXML public TextField GrossAmount;
 @FXML public TextField NetAmount;
 @FXML public TextField PurchaseRate;
 @FXML public TextField Discount;
 @FXML public TextField DiscountPercent;
 @FXML public Button AddCart;
 @FXML public TableView < Items > table;
 @FXML public TableColumn < Items, String > ProductColumn;
 @FXML public TableColumn < Items, String > SupplierColumn;
 @FXML public TableColumn < Items, Integer > QuantityColumn;
 @FXML public TableColumn < Items, String > BatchColumn;
 @FXML public TableColumn < Items, Float > PRColumn;
 @FXML public TableColumn < Items, Integer > BonusColumn;
 @FXML public TableColumn < Items, Float > DiscountColumn;

 private ObservableList < Items > generateInvoiceEntry() { // generate table      with its inputs values

  newItems.setProductName(ProductBox.getSelectionModel().getSelectedItem().toString());
  newItems.setSupplierName(SupplierBox.getSelectionModel().getSelectedItem().toString());
  newItems.setBatchNo(Batch.getText());
  newItems.setBonus(this.toInteger(Bonus.getText()));
  newItems.setDiscount(this.toFloat(DiscountPercent.getText()));
  newItems.setQuantity(this.toInteger(Quantity.getText()));
  newItems.setPurchaseRate(this.toFloat(PurchaseRate.getText()));
  list.add(newItems);
  return list;
 }

 //................FXML Methods.................

 @FXML public void addCart(ActionEvent event) { // a button for make a cart in invoice table
   try {

    table.setItems(generateInvoiceEntry());
   } catch (NullPointerException e) {
    System.err.println(event.getTarget().toString());
   } //in progress

  } //end addCart method

 public void initialize(URL url, ResourceBundle rb) {

   ProductColumn.setCellValueFactory(cellData -> cellData.getValue().ProductNameProperty());
   SupplierColumn.setCellValueFactory(cellData -> cellData.getValue().SupplierNameProperty());
   QuantityColumn.setCellValueFactory(cellData -> cellData.getValue().QuantityProperty().asObject());
   BatchColumn.setCellValueFactory(cellData -> cellData.getValue().BatchNoProperty());
   PRColumn.setCellValueFactory(cellData -> cellData.getValue().PurchaseRateProperty().asObject());
   BonusColumn.setCellValueFactory(cellData -> cellData.getValue().BonusProperty().asObject());
   DiscountColumn.setCellValueFactory(cellData -> cellData.getValue().DiscountProperty().asObject());

  } //end initialize
} //end class

Items类如下:

public class Items {

 private final SimpleStringProperty ProductName;
 private final SimpleStringProperty SupplierName;
 private final SimpleIntegerProperty Quantity;
 private final SimpleStringProperty Batch;
 private final SimpleIntegerProperty Bonus;
 private final SimpleFloatProperty PurchaseRate;
 private final SimpleFloatProperty Discount;

 /**
  * default constructor...

 */

 public Items() {
  this.ProductName = new SimpleStringProperty();
  this.SupplierName = new SimpleStringProperty();
  this.Batch = new SimpleStringProperty();
  this.Bonus = new SimpleIntegerProperty();
  this.Quantity = new SimpleIntegerProperty();
  this.Discount = new SimpleFloatProperty();
  this.PurchaseRate = new SimpleFloatProperty();
 }


 public String getProductName() {
  return ProductName.get();
 }
 public void setProductName(String product) {
  ProductName.set(product);
 }
 public StringProperty ProductNameProperty() {

  return ProductName;
 }

 public String getSupplierName() {
  return SupplierName.get();
 }
 public void setSupplierName(String name) {
  SupplierName.set(name);
 }

 public StringProperty SupplierNameProperty() {

  return SupplierName;
 }

 public String getBatchNo() {
  return Batch.get();
 }
 public void setBatchNo(String num) {
  Batch.set(num);
 }
 public StringProperty BatchNoProperty() {
  return Batch;
 }

 public int getQuantity() {
  return Quantity.get();
 }
 public void setQuantity(int qnt) {
  Quantity.set(qnt);
 }
 public IntegerProperty QuantityProperty() {
  return this.Quantity;
 }

 public int getBonus() {
  return Bonus.get();
 }
 public void setBonus(int qnt) {
  Bonus.set(qnt);
 }
 public IntegerProperty BonusProperty() {
  return this.Bonus;
 }

 public float getDiscount() {
  return Discount.get();
 }
 public void setDiscount(float qnt) {
  Discount.set(qnt);
 }
 public FloatProperty DiscountProperty() {
  return this.Discount;
 }

 public float getPurchaseRate() {
  return PurchaseRate.get();
 }
 public void setPurchaseRate(float rate) {
  PurchaseRate.set(rate);
 }
 public FloatProperty PurchaseRateProperty() {
  return this.PurchaseRate;
 }
} //end class

我想在每一行添加不同的值,因为我们会获得不同细节的常规发票。

希望我能清楚地解释我的问题。

1 个答案:

答案 0 :(得分:0)

您需要进行此更改,它应该有效:

 private ObservableList < Items > generateInvoiceEntry() { // generate table      with its inputs values
  Items newItems = new Items(); 
  newItems.setProductName(ProductBox.getSelectionModel().getSelectedItem().toString());
  newItems.setSupplierName(SupplierBox.getSelectionModel().getSelectedItem().toString());
  newItems.setBatchNo(Batch.getText());
  newItems.setBonus(this.toInteger(Bonus.getText()));
  newItems.setDiscount(this.toFloat(DiscountPercent.getText()));
  newItems.setQuantity(this.toInteger(Quantity.getText()));
  newItems.setPurchaseRate(this.toFloat(PurchaseRate.getText()));
  list.add(newItems);
  return list;
 }