我无法使用可观察的列表更新表视图。当我运行以下代码时,我得到一个表,其中第一列填充了数据(productid),而其余的则没有。所有PropertyValueFactory实例都使用正确的字符串名称进行调用。
<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;
}
这里是请求的getter和setter。我不明白的是为什么我的productID可以正常工作,但是当代码基本相同时,其他的ID却不能正常工作。