JavaFx 2.x TableView 绑定(bind)列

标签 java data-binding tableview javafx javafx-2

您好,我正在尝试使用 ObservableList 填充我的 JavaFx TableView,如下所示:

 emf = Persistence.createEntityManagerFactory("shopPu");
            em = emf.createEntityManager();
            List<Products> proList = em.createQuery("select p from Products p").getResultList();
            ObservableList<Products> proObs = FXCollections.observableList(proList);
            tableView.setEditable(true);
            tableView.setItems(proObs);

它的工作没有错误,我的列表正在填充数据,但 TableView 没有显示任何内容。

这是我的 FXML

<TableView fx:id="tProducts" prefHeight="246.0" prefWidth="726.0" AnchorPane.bottomAnchor="160.0" AnchorPane.leftAnchor="7.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="70.0">
      <columns>
        <TableColumn text="ID" fx:id="ID"/>
      </columns>
    </TableView>

我尝试过这个:

<TableView fx:id="tProducts" prefHeight="246.0" prefWidth="726.0" AnchorPane.bottomAnchor="160.0" AnchorPane.leftAnchor="7.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="70.0">
      <columns>
        <TableColumn text="ID" fx:id="ID">
            <cellValueFactory>
                <PropertyValueFactory property="id" />
            </cellValueFactory>
        </TableColumn>
      </columns>
    </TableView>

但不幸的是它给出了这样的错误:

javafx.fxml.LoadException: PropertyValueFactory is not a valid type.
    at javafx.fxml.FXMLLoader.createElement(Unknown Source)
    at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)

请帮忙如何填充TableView

更新

Controller :

public class MainWindow implements Initializable {

    @FXML private Label lblStatus;
    @FXML private TableView<Products> tableView;
    private EntityManager em;
    private EntityManagerFactory emf;

    @FXML
    private void Refresh_Clicked(javafx.event.ActionEvent event) {
        try {
            emf = Persistence.createEntityManagerFactory("shopPu");
            em = emf.createEntityManager();
            List<Products> proList = em.createQuery("select p from Products p").getResultList();

            ObservableList<Products> proObs = FXCollections.observableList(proList);

            tableView.setEditable(true);
            tableView.setItems(proObs);
        } catch (Exception e) {

                JOptionPane.showMessageDialog(null, e.getMessage());

        }

    }

谢谢。

最佳答案

根据您的FXML,您的TableView 应该命名为tProducts。但这应该会在注入(inject)过程中产生错误,您可以粘贴 Controller 代码吗?

关于JavaFx 2.x TableView 绑定(bind)列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12259748/

相关文章:

java - 实现linkedStack时出现NullPointerException

android - android绑定(bind)数据的通用形式

swift - 将 UITableView 约束为 InputAccessory

java - 禁止将项目放入同一个 TableView 中

java - 如果 java flyway 迁移抛出异常会怎样?

java - java 中数据计数的最佳实践

Java - 我应该读取 setter 中的值吗?

Android Data Binding疑惑

android - @BidingMethod 不适用于方法

java - 无法在 JavaFX 中为我的 TableColumn 设置不同的值