Can't populate JavaFX TableView created in SceneBuilder 2

北战南征 提交于 2019-12-03 21:48:16

You haven't set any cellValueFactorys on your TableColumns (Example 12-5 in the tutorial you linked).

You can do this in FXML with

<TableColumn fx:id="firstNameCol" prefWidth="75.0" text="First Name" >
    <cellValueFactory><PropertyValueFactory property="firstName"/></cellValueFactory>
</TableColumn>

or in the controller with

firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));

(and similarly for the other columns)

You've actually made two mistakes.

First of all, you're trying to assign the datamodel to the TableView using getItems().setAll(). Take a look at the documentation of setAll(). It copies all the values of the given list into the ObservableList. This is not what you want, you want the TableView to start observing data and responding to changes. The way to do this is to assign a reference directly, through setItems(data).

Take a look at the code examples in the documentation of TableView. You forgot to connect the various StringProperties in Person up to the columns. This is the way they do it:

firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));

Lastly, I've got a little sidenote. Note that this is NOT the cause of your specific issue.

I see you're using raw types. This is generally considered a bad thing, and you're probably getting some warnings from the compiler about it. You can solve the issue by stating the generic types your objects use. Notice in the above code sample, I state for the PropertyValueFactories that the type of class contained within the TableView.items is Person and that the type of object displayed in the rows is String.

As for scrolling, which you also asked for, it does that automatically once there are enough lines in the TableView.

IN handle Mouse Button you can write: /setItems(); follow the code of green tick above

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!