javafx tableview:添加新条目更改所有以前的数据
javafx tableview: add new entry change all previous data
每当在 table-view 中添加新行时,它都会更改 table 中所有先前的数据并使所有值相同。下图为示例:
在上面的采购发票中,table 中的最后添加更改了前几行的值。
相关代码部分是....
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
class 如下:
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
我想在每一行中添加不同的值,因为我们会收到不同细节的常规发票。
希望我已经清楚地解释了我的问题。
您需要进行此更改,它应该会起作用:
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;
}
每当在 table-view 中添加新行时,它都会更改 table 中所有先前的数据并使所有值相同。下图为示例:
在上面的采购发票中,table 中的最后添加更改了前几行的值。
相关代码部分是....
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
class 如下:
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
我想在每一行中添加不同的值,因为我们会收到不同细节的常规发票。
希望我已经清楚地解释了我的问题。
您需要进行此更改,它应该会起作用:
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;
}