【发布时间】:2016-06-14 11:51:21
【问题描述】:
我正在尝试使用 ObservableList 中的数据填充 TableView。我以前做过这个,但由于某种原因,我现在无法让它工作。我没有得到任何异常或任何东西,但它根本没有向表中添加任何东西。
this 的问题类似,但我发现了另一个由 JPA 引起的问题,因此提到的 Modification 的构造函数永远不会执行。取而代之的是,JPA 施展魔法来分配值。
这是我的代码 - 我剪掉了与问题无关的代码:
FXML
<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>
Main.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();
}
}
Modification.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);
}
}
我们将不胜感激任何解决此问题的帮助!
【问题讨论】:
-
另外:
private TableColumn<ObservableList<Modification>, String> colModArt应为private TableColumn<Modification, String> colModArt- 这对每个TableColumn定义都有效。 -
@DVarga 谢谢!我确实更改了
PropertyValueFactory中的名称,这使它获得了正确的信息。然后我意识到构造函数永远不会被调用,因为 JPA 使用另一个构造函数创建对象。我只是为这些值创建了一个 get 方法,而不是全部修复。
标签: java javafx tableview observablelist