【问题标题】:JavaFX populate TableView from ObservableListJavaFX 从 ObservableList 填充 TableView
【发布时间】: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&lt;ObservableList&lt;Modification&gt;, String&gt; colModArt 应为 private TableColumn&lt;Modification, String&gt; colModArt - 这对每个 TableColumn 定义都有效。
  • @DVarga 谢谢!我确实更改了PropertyValueFactory 中的名称,这使它获得了正确的信息。然后我意识到构造函数永远不会被调用,因为 JPA 使用另一个构造函数创建对象。我只是为这些值创建了一个 get 方法,而不是全部修复。

标签: java javafx tableview observablelist


【解决方案1】:

看来这个问题部分是由 JPA 引起的。

首先,我在PropertyValueFactory 中使用了不正确的字段名称。我不得不从它们中删除 Property 部分,因为显然这是自动添加的(反射?)

其次,JPA 实现从未执行过Modification 构造函数,因此SimpleStringProperty 从未得到它的值。我通过为这些值编写自己的 getter 解决了这个问题:getProductNamegetProductBarcode

【讨论】:

  • 你的构造函数没有被调用可能是件好事,因为它会抛出一个NullPointerException...要了解PropertyValueFactory是如何工作的,请阅读documentation跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 2018-10-14
  • 2013-06-06
相关资源
最近更新 更多