如何使用FXML和JavaFX动态填充TableView [英] How to populate TableView dynamically with FXML and JavaFX
问题描述
如何在FXML中定义表,然后使用JavaFX代码在运行时动态填充表?
How do I define my table in FXML and then use my JavaFX code to populate it dynamically at runtime?
推荐答案
- 在fxml文件中定义TableView.几件事要注意:
- 根目录应具有与之关联的控制器类.
- TableView和TableColumn应该具有指定的fx:id属性.
- Define the TableView in the fxml file. Few things to note:
- the root should have a controller class associated with it.
- the TableView and TableColumn should have fx:id attributes specified.
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.MyController">
<center>
<ScrollPane disable="false" visible="true">
<content>
<TableView fx:id="myTableView" prefHeight="-1.0" prefWidth="-1.0">
<columns>
<TableColumn fx:id="idColumn" prefWidth="100.0" text="Id" />
</columns>
</TableView>
</content>
</ScrollPane>
</center>
</BorderPane>
<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.MyController">
<center>
<ScrollPane disable="false" visible="true">
<content>
<TableView fx:id="myTableView" prefHeight="-1.0" prefWidth="-1.0">
<columns>
<TableColumn fx:id="idColumn" prefWidth="100.0" text="Id" />
</columns>
</TableView>
</content>
</ScrollPane>
</center>
</BorderPane>
-
定义控制器类.几件事要注意:
Define the controller class. Few things to note:
- 变量应与@FXML标记和一个新对象关联 不应创建TableView/TableColumn.
- 变量的名称应与fx:id的相应属性值相同,如下所示: 在fxml中提到过.
- 控制器类应实现javafx.fxml.Initializable,因此应定义方法public void initialize(URL位置,ResourceBundle资源)
-
数据模型类MyDataModel类用于填充数据.
- the variables should be linked with the @FXML tag and a new object of TableView/TableColumn should not be created.
- the variables should be named the same as the corresponding attribute value of fx:id as mentioned in the fxml.
- the controller class should implement javafx.fxml.Initializable and hence should define the method public void initialize(URL location, ResourceBundle resources)
a class Data Model class MyDataModel is used to populate the data.
公共类MyController实现可初始化的{
public class MyController implements Initializable {
@FXML
private TableView<MyDataModel> myTableView;
@FXML
private TableColumn<MyDataModel, String> idColumn;
@Override
public void initialize(URL location, ResourceBundle resources) {
idColumn.setCellValueFactory(new PropertyValueFactory<MyDataModel, String>"idColumn"));
myTableView.getItems().setAll(getItemsToAdd());
}
private List<MyDataModel> getItemsToAdd(){
// this method would fetch the necessary items from database.
}
}
定义数据模型类.几件事要注意:
Define the Data Model class. Few things to note:
- 该变量应命名为idColumnProperty,因为为PropertyValueFactory传递的字符串是"idColumn".
- 该变量必须是私有的最终SimpleStringProperty,因为在控制器类中提到的类型是该列的String.
-
模型类必须具有方法getIdColumn()和setIdColumn(String id)
- the variable should be named as idColumnProperty as the string passed for PropertyValueFactory is "idColumn".
- the variable must be private final SimpleStringProperty as the type mentioned in controller class is String for the column.
the model class must have the methods getIdColumn() and setIdColumn(String id)
公共类MyDataModel {
public class MyDataModel {
private final SimpleStringProperty idColumnProperty = new SimpleStringProperty("");
public MyDataModel(){
this("");
}
public MyDataModel(String id){
setIdColumn(id);
}
public String getIdColumn(){
idColumnProperty.get();
}
public void setIdColumn(String id){
idColumnProperty.set(id);
}
}
这篇关于如何使用FXML和JavaFX动态填充TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!