【问题标题】:JavaFx 2.x TableView binding columnsJavaFx 2.x TableView 绑定列
【发布时间】:2012-08-28 21:25:23
【问题描述】:

您好,我正在尝试使用 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

更新

控制器:

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());

        }

    }

谢谢。

【问题讨论】:

标签: java data-binding tableview javafx javafx-2


【解决方案1】:

根据您的FXML,您的TableView 应命名为tProducts。但这应该会在注入过程中产生错误,请您粘贴控制器代码吗?

【讨论】:

  • 所以在您的 FXML 中您有 fx:id="tProducts",但在您的控制器中您正尝试注入名为 tableView 的对象。 fx:id 必须与注入的变量名相同。尝试将fx:id 更改为“tableView”。还可以尝试使用第二个 FXML,即 cellValueFactory
【解决方案2】:

如果不进一步分析您的代码示例,发布的错误可能是由于 PropertyValueFactory 的 fxml 中缺少导入造成的。

【讨论】:

    【解决方案3】:

    您必须在代码中为您的 TableView 和 fxid: 设置相同的名称。

    <TableView fx:id="table" layoutX="27.0" layoutY="65.0" prefHeight="193.0" prefWidth="552.0">
    
    TableView table;
    

    您必须初始化表格列...并定义它们的属性。

    试试这个..

     @FXML
    private TableColumn<CheckDo, String> username;
    username.setCellValueFactory(new PropertyValueFactory<CheckDo,String>("username"));
    

    你必须设置列的 setvaluefactory 才能显示它的值

    【讨论】:

      【解决方案4】:

      我在我的项目中解决了一个语义问题,在 FXML 文件中添加了这一行。

      <?import javafx.scene.control.cell.PropertyValueFactory ?>
      

      【讨论】:

        猜你喜欢
        • 2012-05-28
        • 2014-08-06
        • 2013-09-08
        • 1970-01-01
        • 2023-03-12
        • 2012-06-17
        • 2013-03-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多