I would like to know how do I populate a TableView with data... All the examples I have seen creates a TableView with columns and everything and add it to the scene. All done in the java code itself.
What I want to know: if I design my "form" in JavaFx Scene builder. Defining all the tables and columns in there. How do I access it to populate it from java? Or if someone can point me to a proper tutorial on this please.
I have defined my form in JavaFx Scene Builder - only a TableView with 3 Columns
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.paint.*?> <AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="288.0" prefWidth="293.0" xmlns:fx="http://javafx.com/fxml"> <children> <TableView fx:id="tableView" layoutX="35.0" layoutY="28.0" prefHeight="200.0" prefWidth="227.0"> <columns> <TableColumn prefWidth="75.0" text="UserId" fx:id="UserId" /> <TableColumn prefWidth="75.0" text="UserName" fx:id="UserName" /> <TableColumn prefWidth="75.0" text="Active" fx:id="Active" /> </columns> </TableView> </children> </AnchorPane>
I have my data in a ResultSet in my Java code.
ResultSet rs = c.createStatement().executeQuery(SQL);
I need to populate the TableView.
Thanks
FXML Controller Classes There are two ways to set a controller for an FXML file. The first way to set a controller is to specify it inside the FXML file. The second way is to set an instance of the controller class on the FXMLLoader instance used to load the FXML document.
The TableView class provides built-in capabilities to sort data in columns. Users can alter the order of data by clicking column headers. The first click enables the ascending sorting order, the second click enables descending sorting order, and the third click disables sorting. By default, no sorting is applied.
The <fx:include> tag can be used to include one fxml file into another. The controller of the included fxml can be injected into the controller of the including file just as any other object created by the FXMLLoader . This is done by adding the fx:id attribute to the <fx:include> element.
To access to tableview you need to define a controller of your FXML page. Add
fx:controller="path.to.MyController"
attribute to the AnchorPane
in FXML file. Then create the controller and link TableView, TableColumns from FXML file by putting @FXML
annotation in front of these variables:
package path.to; public class MyController implements Initializable { @FXML private TableView<User> tableView; @FXML private TableColumn<User, String> UserId; @FXML private TableColumn<User, String> UserName; @FXML private TableColumn<User, String> Active; @Override public void initialize(URL location, ResourceBundle resources) { UserId.setCellValueFactory(new PropertyValueFactory<User, String>("id")); UserName.setCellValueFactory(new PropertyValueFactory<User, String>("name")); Active.setCellValueFactory(new PropertyValueFactory<User, String>("active")); tableView.getItems().setAll(parseUserList()); } private List<User> parseUserList(){ // parse and construct User datamodel list by looping your ResultSet rs // and return the list } }
The tableview is populated in the initialize
method. Note that in controller we are not creating new tableview or tablecolumns, since they are already created whlle the FXML file is being loaded. Also note that the TableView and Tablecolumn variable names must be the same with fx:id
values in the corresponding FXML file. So while UserId, UserName and Active names are not convenient namings, change them both in FXML file and in Controller to names like userIdCol, userNameCol and userActiveCol respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With