JavaFx 2.x TableView binding columns

2019-08-29 10:52发布

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.

4条回答
家丑人穷心不美
2楼-- · 2019-08-29 10:55

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

查看更多
老娘就宠你
3楼-- · 2019-08-29 10:55

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

查看更多
我想做一个坏孩纸
4楼-- · 2019-08-29 11:15

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

<?import javafx.scene.control.cell.PropertyValueFactory ?>
查看更多
干净又极端
5楼-- · 2019-08-29 11:19

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

查看更多
登录 后发表回答