问题描述
我想知道如何用数据填充TableView ...我所看到的所有示例都创建了一个包含列和所有内容的TableView并将其添加到场景中.全部在Java代码本身中完成.
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.
我想知道的是:如果我在JavaFx Scene构建器中设计表单".定义其中的所有表和列.我如何访问它以从Java填充它?或者,如果有人可以给我指出有关此的适当教程.
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.
我已经在JavaFx Scene Builder中定义了我的表单-只有一个包含3列的TableView
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>
我将数据存储在Java代码的ResultSet中.
I have my data in a ResultSet in my Java code.
ResultSet rs = c.createStatement().executeQuery(SQL);
我需要填充TableView.
I need to populate the TableView.
谢谢
推荐答案
要访问表视图,您需要定义FXML页面的控制器.添加
To access to tableview you need to define a controller of your FXML page. Add
fx:controller="path.to.MyController"
归因于FXML文件中的AnchorPane
.然后创建控制器,并通过在这些变量的前面放置@FXML
批注来链接FXML文件中的TableView,TableColumns:
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
}
}
tableview在initialize
方法中填充.请注意,在控制器中我们不会创建新的tableview或tablecolumns,因为已经在加载FXML文件时创建了它们.还要注意,TableView和Tablecolumn变量名称必须与相应FXML文件中的fx:id
值相同.因此,虽然UserId,UserName和Active名称不是方便的命名,但是在FXML文件和Controller中将它们分别更改为诸如userIdCol,userNameCol和userActiveCol之类的名称.
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.
这篇关于如何填充在JavaFx Scene Builder中设计的fxml文件中定义的TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!