0

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;
    }
}

MainScene FXML file: scenebuilder

Thanks in advance.

b0dan
  • 109
  • 3
  • 5
    I suggest that you use lambda's like [this example](https://stackoverflow.com/questions/70175587/how-do-you-use-a-javafx-tableview-with-java-records). With a lambda you get compile time type checking and identifier matching, rather than runtime failures for type and name mismatches with PropertyValueFactory. – jewelsea Apr 21 '22 at 20:39
  • 1
    [mcve] please.. – kleopatra Apr 21 '22 at 22:05
  • @jewelsea - That did the trick. Thanks! – b0dan Apr 25 '22 at 17:43

1 Answers1

0

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

MMR
  • 69
  • 1
  • 7
Giovanni Contreras
  • 2,345
  • 1
  • 13
  • 22