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.
According to yours
FXML
yourTableView
should by namedtProducts
. But this should create error during injection, could You please paste controller code?Without analyzing your code example any further, the posted error is probably caused by a missing import in the fxml for PropertyValueFactory.
I solved a semelhant problem in my project adding this line in the FXML file.
you have to make same name of your TableView and fxid: in code.
you have to initalize table columns...and define their property.
try this..
you have to set setvaluefactory of columns for show values of it