0

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.

user525717
  • 1,612
  • 5
  • 28
  • 40
  • 1
    Maybe this can be useful: http://stackoverflow.com/questions/11976461/javafx2-2-not-all-table-columns-showing-data-with-propertyvaluefactory – Perneel Sep 04 '12 at 21:10

4 Answers4

1

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

airborn
  • 2,547
  • 1
  • 15
  • 12
  • So in your FXML you have `fx:id="tProducts"`, but in yours controller You are trying to inject object named `tableView`. `fx:id` must be the same as injected variable name. Try changing `fx:id` to 'tableView'. Also try using the second FXML, the one with `cellValueFactory` – airborn Sep 05 '12 at 07:29
1

Without analyzing your code example any further, the posted error is probably caused by a missing import in the fxml for PropertyValueFactory.

Werner Lehmann
  • 911
  • 1
  • 8
  • 14
1

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

<?import javafx.scene.control.cell.PropertyValueFactory ?>
Saket Mittal
  • 3,726
  • 3
  • 29
  • 49
0

you have to make same name of your TableView and fxid: in code.

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

TableView table;

you have to initalize table columns...and define their property.

try this..

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

you have to set setvaluefactory of columns for show values of it

Anshul Parashar
  • 3,096
  • 4
  • 30
  • 56