JavaFX: Blank rows in FXML TableView

859 Views Asked by At

I've been trying to add a row of information from my Product class to this tableView:

Empty Table

When I hit the add button and run:

productTableView.getItems().add(new Product(...)

It adds a blank row to my table:

Table After Add()

Controller.java

import javafx.scene.control.TableView;

public class Controller {

// Initialized all elements of GUI
public TableView<Product> productTableView;
public TableColumn<Product, Integer> productID;
public TableColumn<Product, String> name;
public TableColumn<Product, Double> price;
public TableColumn<Product, Integer> quantity;
public TableColumn<Product, Double> cost;
public TableColumn<Product, Date> bought;
public TableColumn<Product, Date> sold;
public TableColumn<Product, Integer> threshold;                 

public void addProduct() {                                           
      productTableView.getItems().add(new Product( 1, "Toilet Paper", 9.99, 1.25, 300, 300, new Date(), new Date())
    );
}

Inventory_Management.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<?import javafx.scene.control.cell.PropertyValueFactory?>
<VBox alignment="TOP_RIGHT" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Inventory_Management.Controller">
   <children>
      <TableView fx:id="productTableView" VBox.vgrow="ALWAYS">
         <columns>
             <TableColumn fx:id="productID" prefWidth="75.0" text="Product ID">
                <cellValueFactory>
                    <PropertyValueFactory property="productID" />
                </cellValueFactory>
            </TableColumn>

             <TableColumn fx:id="name" prefWidth="75.0" text="Name">
             <cellValueFactory>
                 <PropertyValueFactory property="name" />
             </cellValueFactory>
             </TableColumn>

            <TableColumn fx:id="price" prefWidth="75.0" text="Price">
             <cellValueFactory>
                 <PropertyValueFactory property="price" />
             </cellValueFactory>
            </TableColumn>

            <TableColumn fx:id="quantity" prefWidth="75.0" text="Quantity">
             <cellValueFactory>
                 <PropertyValueFactory property="quantity" />
             </cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="cost" prefWidth="75.0" text="Cost">
             <cellValueFactory>
                 <PropertyValueFactory property="cost" />
             </cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="bought" prefWidth="75.0" text="Bought">
             <cellValueFactory>
                 <PropertyValueFactory property="bought" />
             </cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="sold" prefWidth="75.0" text="Sold">
             <cellValueFactory>
                 <PropertyValueFactory property="sold" />
             </cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="threshold" prefWidth="75.0" text="Threshold">
             <cellValueFactory>
                 <PropertyValueFactory property="threshold" />
             </cellValueFactory>
            </TableColumn>
         </columns>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>
      </TableView>
   </children>
   <padding>
      <Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
   </padding>
</VBox>

I built the FXML using Scene Builder. I'm not sure how to implement the rows without creating the table programmatically. The data I input should be appearing in the table, but I'm not sure if its because of the way I'm implementing the row, or if there is something off with my PropertyValues.

1

There are 1 best solutions below

0
On

The issue was that in my Product class, I did not have the appropriate getters and setters.

My getters in this case were case sensitive.

Example of "bad code":

public class Product {

    private int productID;

    public int getID() {
       return productID;
    }
}

Example of working code:

public int getProductID() {
    return productID;
}

A small difference, but important.