0

I am having trouble updating a tableview with an observable list. When I run the below code I get a table with the first column populated with data (productid) but the rest are not. All PropertyValueFactory instances are called with the proper string name.

<TableView fx:id="productTable" layoutX="92.0" layoutY="319.0" prefHeight="97.0" prefWidth="363.0" GridPane.columnIndex="7" GridPane.columnSpan="5" GridPane.rowIndex="3" GridPane.rowSpan="2">
 <columns>
    <TableColumn prefWidth="98.0" text="Product ID" fx:id="productID"/>
    <TableColumn prefWidth="110.0" text="Product Name" fx:id="productName"/>
    <TableColumn prefWidth="131.0" text="Inventory Level" fx:id="productInventoryLevel"/>
    <TableColumn prefWidth="128.0" text="Price per Unit" fx:id="productPrice"/>
 </columns>
</TableView>
//Controller.java
@FXML private TableView<Product> productTable;
@FXML private TableColumn<Product, String> productID;
@FXML private TableColumn<Product, String>  productPrice;
@FXML private TableColumn<Product, String>  productName;
@FXML private TableColumn<Product, String>  productInventoryLevel;
private ObservableList<Product> productData = FXCollections.observableArrayList(
        new Product(0, "Wheel", 100.99, 4, 0, 1), 
        new Product(1, "Seat", 50.0, 4, 0, 1));
//Product properties declared the same as         https://docs.oracle.com/javafx/2/ui_controls/table-view.htm#
 private ObservableList<Product> productData = FXCollections.observableArrayList(
    new Product(0, "Wheel", 100.99, 4, 0, 1), 
    new Product(1, "Seat", 50.0, 4, 0, 1));

 @Override
 public void initialize(URL url, ResourceBundle rb) {
// TODO
     System.out.println("Product ID factories");

     productID.setCellValueFactory(new PropertyValueFactory<Product, String>("productID"));
     productPrice.setCellValueFactory(new PropertyValueFactory<Product, String>("productPrice"));
     productInventoryLevel.setCellValueFactory(new PropertyValueFactory<Product, String>("productInventoryLevel"));
     productName.setCellValueFactory(new PropertyValueFactory<Product, String>("productName"));
     productTable.setItems(productData);

  System.out.println("Set items");

 }
 //product constructor
public Product(int id, String name, double price, int inStock, int min, int max){
    associatedParts = new ArrayList<>();
    this.productID = new SimpleIntegerProperty(id);
    this.productName = new SimpleStringProperty(name);
    this.productPrice = new SimpleDoubleProperty(price);
    this.productInStock = new SimpleIntegerProperty(inStock);
    this.min = new SimpleIntegerProperty(min);
    this.max = new SimpleIntegerProperty(max);
}
public void setName(String name) { productName.set(name); }
public String getName() { return productName.get(); }

public void setPrice(double price) { productPrice.set(price); }
public double getPrice() { return productPrice.get(); }

public int getInStock() { return productInStock.get(); }
public void setInStock(int count) { productInStock.set(count); }

public void setMin(int min) { this.min.set(min); }
public int getMin() { return this.min.get(); }

public void setMax(int max) { this.max.set(max); }
public int getMax() { return this.max.get(); }

public void setProductID(int productId) { productID.set(productId); }
public int getProductID() { return productID.get(); }

public void addAssociatedPart(Object part) { associatedParts.add(part); }
public boolean removeAssociatedPart(int partID){

    return false;
}

Here is the getters and setters as requested.What I don't understand is why my productID is working but the others are not when the code is basically identical.

  • _All PropertyValueFactory instances are called with the proper string name_ most probably not ;) – kleopatra Jul 17 '18 at 10:32
  • Edited to show how they are declared. – Tyler Johnson Jul 17 '18 at 10:41
  • that snippet is irrelevant and proves exactly nothing ;) Please read http://stackoverflow.com/help/mcve and act accordingly. – kleopatra Jul 17 '18 at 10:54
  • 1
    The `Product` constructor does not provide the necessary info, since `PropertyValueFactory` accesses the properties via methods (`Property()` or `get()`, e.g. `public StringProperty productName() {return productName;}`). – fabian Jul 17 '18 at 11:00
  • I don't really know why do much people are using the `PropertyValuerFactory` way instead of the `CallBack` way. Check this question and its answer : https://stackoverflow.com/questions/38049734/java-setcellvaluefactory-lambda-vs-propertyvaluefactory-advantages-disadvant. It can explain you how to use both the Callback and the `PropertyValueFactory` even if you would stick to it. – Sunflame Jul 17 '18 at 12:02

0 Answers0