JavaFx 2.x TableView binding columns

data-bindingjavajavafxjavafx-2tableview

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.

Best Answer

According to yours FXML your TableView should by named tProducts. But this should create error during injection, could You please paste controller code?