'TableView Not Being Populated [duplicate]
Does anyone have any idea why the table is not getting populated with the products?
MainController class:
//imports
Public class MainController implements Initializable {
@FXML
private TableView<Product> table;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
TableColumn<Product, Long> prIDColumn = new TableColumn<Product, Long>("PrID");
prIDColumn.setCellValueFactory(new PropertyValueFactory<Product, Long>("productID"));
TableColumn<Product, String> productColumn = new TableColumn<Product, String>("Product");
productColumn.setCellValueFactory(new PropertyValueFactory<Product, String>("productName"));
TableColumn<Product, Integer> quantityColumn = new TableColumn<Product, Integer>("Quantity");
quantityColumn.setCellValueFactory(new PropertyValueFactory<Product, Integer>("productQuantity"));
TableColumn<Product, Integer> taxColumn = new TableColumn<Product, Integer>("Tax%");
taxColumn.setCellValueFactory(new PropertyValueFactory<Product, Integer>("taxPercentage"));
TableColumn<Product, Integer> priceColumn = new TableColumn<Product, Integer>("Price");
priceColumn.setCellValueFactory(new PropertyValueFactory<Product, Integer>("totalPrice"));
table.getColumns().add(prIDColumn);
table.getColumns().add(productColumn);
table.getColumns().add(quantityColumn);
table.getColumns().add(taxColumn);
table.getColumns().add(priceColumn);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
}
//other unrelated code
public void addProducts(ActionEvent event) { //Linked with the "Add Product" button.
try {
table.getItems().add(new Product(1234, "Mayo", 1, 5, 100));
table.getItems().add(new Product(2345, "Ketchup", 2, 3, 90));
} catch(Exception e) {
System.err.println(e);
}
}
}
Product class:
public class Product {
private int productID;
private String productName;
private int productQuantity;
private int taxPercentage;
private int totalPrice;
public Product(int productID, String productName, int productQuantity, int taxPercentage, int totalPrice) {
this.productID = productID;
this.productName = productName;
this.productQuantity = productQuantity;
this.taxPercentage = taxPercentage;
this.totalPrice = totalPrice;
}
public int getProductID() {
return productID;
}
public String getProductName() {
return productName;
}
public int getProductQuantity() {
return productQuantity;
}
public int getTaxPercentage() {
return taxPercentage;
}
public int getTotalPrice() {
return totalPrice;
}
public void setProductID(int productID) {
this.productID = productID;
}
public void setProductName(String productName) {
this.productName = productName;
}
public void setProductQuantity(int productQuantity) {
this.productQuantity = productQuantity;
}
public void setTaxPercentage(int taxPercentage) {
this.taxPercentage = taxPercentage;
}
public void setTotalPrice(int totalPrice) {
this.totalPrice = totalPrice;
}
}
Thanks in advance.
Solution 1:[1]
you have
TableColumn<Product, Long> prIDColumn = new TableColumn<Product, Long>("PrID");
prIDColumn.setCellValueFactory(new PropertyValueFactory<Product, Long>("productID"));
but productID is an int not a long in product class
then
TableColumn<Product, Integer> prIDColumn = new TableColumn<Product,Integer>("PrID");
prIDColumn.setCellValueFactory(new PropertyValueFactory<Product, Integer>("productID"));
Or change productID from int to long in Product class
its better you to use StringProperty or IntegerProperty rather than int or String in your model for work with TableView
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MMR |

