Reputation: 55
I'm making a program that uses a tableview. Everything is going smoothly, except for some reason I can't get the Integer values to populate in the table. Below is my code in the main program, the String and Double populate when I run the program, but the Integer doesn't. In my Product class, the sku is an int. Not really sure what is wrong, looking for some insight!
TableView<Product> tvOrderDetails = new TableView<>();
ObservableList<Product> ol1 = listGen(invoice);
tvOrderDetails.setItems(ol1);
TableColumn<Product, Integer> colItemNum = new TableColumn<>("Item#");
colItemNum.setCellValueFactory(new PropertyValueFactory("sku"));
TableColumn<Product, String> colDesc = new TableColumn<>("Description");
colDesc.setCellValueFactory(new PropertyValueFactory("name"));
TableColumn<Product, Double> colPrice = new TableColumn<>("Price");
colPrice.setCellValueFactory(new PropertyValueFactory("price"));
tvOrderDetails.getColumns().setAll(colItemNum, colDesc, colPrice);
Basically, I create a tableview of type product. Make an observable list out of the invoice (which is an arraylist of products), then make the columns and add them to the tableview. The Description and Price both populate just fine, but not sku (Integer).
This is my Product Class.
public class Product {
private String name;
private int sku;
private double price;
public String getName() { return name; }
public int getSKU() { return sku; }
public double getPrice() { return price; }
public void setName(String n) { name = n; }
public void setSKU(int s) { sku = s; }
public void setPrice(double p) { price = p; }
Product(String n, int s, double p) {
name = n;
sku = s;
price = p;
}
@Override
public String toString() {
return "\nName: " + name + "\nSKU: " + sku + "\nPrice" + price;
}
public boolean equals(Product two) {
if (this.name.equals(two.getName()) && this.sku == two.getSKU() && this.price == two.getPrice())
return true;
else
return false;
}
}
Here is listGen()
public ObservableList<Product> listGen(Invoice i) {
ObservableList<Product> temp = FXCollections.observableArrayList();
for (int p = 0; p < i.getArray().size(); p++)
temp.add(i.getArray().get(p));
return temp;
}
Here is the Invoice object and adding the products.
Invoice invoice = new Invoice(new Customer());
invoice.add(new Product("Hammer", 30042, 7.95));
invoice.add(new Product("Drill", 30041, 59.99));
Upvotes: 1
Views: 1587
Reputation: 55
Well I dug, and I dug and I dug and after trying everything I finally got it to work. Here's the code.
TableColumn<Product, Integer> colItemNum = new TableColumn<>("Item#");
colItemNum.setCellValueFactory(cellData -> new SimpleIntegerProperty(cellData.getValue().getSKU()).asObject());
Not really sure if this is the right way of doing it, but for now I'm gonna roll with it.
Upvotes: 1