如何在FXML中定义表,然后使用JavaFX代码在运行时动态填充表?
<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>
定义控制器类。几件事要注意:
数据模型类MyDataModel类用于填充数据。
公共类MyController实现了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.
}
}
定义数据模型类。几件事要注意:
模型类必须具有方法getIdColumn()和setIdColumn(String id)
公共类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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句