我正在尝试用来自 ObservableList 的数据填充 TableView。我以前做过这个,但由于某种原因我现在无法让它工作。我没有收到任何例外或任何东西,但它根本没有增加任何东西。
这个问题是类似的,但我发现了由 JPA 引起的另一个问题,这使得它使上述Modification
构造函数永远不会被执行。相反,JPA 会神奇地分配值。
这是我的代码 - 我截取了与问题无关的代码:
福美
<TableView fx:id="tblModifications" layoutX="14.0" layoutY="14.0" prefHeight="545.0" prefWidth="512.0">
<columns>
<TableColumn prefWidth="75.0" text="%itemNumber" fx:id="colModArt"/>
<TableColumn prefWidth="75.0" text="%name" fx:id="colModName"/>
<TableColumn prefWidth="75.0" text="%amount" fx:id="colModAmount" />
</columns>
</TableView>
主.java
public class Main extends Application implements Initializable {
private TableView<Modification> tblModifications;
private TableColumn<ObservableList<Modification>, String> colModArt;
private TableColumn<ObservableList<Modification>, String> colModName;
private TableColumn<ObservableList<Modification>, Integer> colModAmount;
@Override
public void initialize(URL arg0, ResourceBundle arg1){
colModArt.setCellValueFactory(new PropertyValueFactory<>("barcodeProperty"));
colModName.setCellValueFactory(new PropertyValueFactory<>("nameProperty"));
colModAmount.setCellValueFactory(new PropertyValueFactory<>("amountProperty"));
admin = Administration.getInstance();
tblModifications.setItems(admin.observableModifications); // This list is populated with correct data, I tested.
}
@Override
public void start(Stage primaryStage) throws Exception{
resources = ResourceBundle.getBundle("strings", new Locale("NL"));
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("main.fxml"), resources);
primaryStage.setTitle(resources.getString("title"));
primaryStage.setScene(new Scene(root, 1280, 1024));
primaryStage.show();
}
}
修改.java
public class Modification {
public SimpleStringProperty barcodeProperty;
public SimpleStringProperty nameProperty;
public SimpleIntegerProperty amountProperty;
private int id;
private Seller seller;
private Product product;
private int amount;
private Boolean accepted;
public Modification(int id, Seller seller, Product product, int amount) {
this.id = id;
this.seller = seller;
this.product = product;
this.amount = amount;
this.accepted = false;
barcodeProperty.set(String.valueOf(product.getBarcode()));
nameProperty.set(product.getName());
amountProperty.set(amount);
}
}
任何解决此问题的帮助将不胜感激!
这个问题似乎部分是由 JPA 引起的。
首先,我在PropertyValueFactory
中使用了不正确的字段名称。我不得不从他们中删除Property
部分,因为显然这是自动添加的(反射?
其次,JPA 实现从未执行过 Modification
构造函数,因此SimpleStringProperty
从未获得其值。我通过为这些值编写自己的 getter 来解决这个问题:getProductName
和 getProductBarcode