JavaFx 2.x TableView绑定列 [英] JavaFx 2.x TableView binding columns

查看:587
本文介绍了JavaFx 2.x TableView绑定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我试图用ObservableList填充我的JavaFx TableView,如下所示:

  emf = Persistence.createEntityManagerFactory(shopPu ); 
em = emf.createEntityManager();
列表<产品> proList = em.createQuery(从产品p中选择p)getResultList();
ObservableList< Products> proObs = FXCollections.observableList(proList);
tableView.setEditable(true);
tableView.setItems(proObs);

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

这是我的FXML

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

我尝试过:

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

但没有运气它给出这样的错误:

  javafx.fxml.LoadException:PropertyValueFactory不是有效的类型。 
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();
列表<产品> proList = em.createQuery(从产品p中选择p)getResultList();

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

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

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

}

}

谢谢。 / p>

解决方案

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

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


Hello i`m trying to populate my JavaFx TableView with ObservableList like this:

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

Its works without an error my List is filling with data but TableView does not showing anything.

Here is my 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>

i tried this:

<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>

But no luck its gives such error:

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

Please help how to populate TableView

UPDATE

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

        }

    }

Thanks.

解决方案

I solved a semelhant problem in my project adding this line in the FXML file.

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

这篇关于JavaFx 2.x TableView绑定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆