在javafx中,可以使用cellfactory来进行赋初值
下面我将举例进行说明
首先当对应的fxml文件被调用时,他里面的相对应的controller中的intiablize方法将被自动调用,
@FXML
private void initialize() {
// Initialize the person table with the two columns.
firstNameColumn.setCellValueFactory(
cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(
cellData -> cellData.getValue().lastNameProperty());
// Clear person details.
showPersonDetails(null);
// Listen for selection changes and show the person details when changed.
personTable.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showPersonDetails(newValue));
}
需要添加@Fxml以让程序能识别。
其中的cellData是我们同过mainAPP把值传给了tableBiew,cellFactory将tableView中的每一行都填充数据。
mainAPP中将值传给tableVIew
controller.setMainApp(this);
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
// Add observable list data to the table
personTable.setItems(mainApp.getPersonData());
}
public ObservableList<Person> getPersonData() {
return personData;
}
public MainApp() {
// Add some sample data
personData.add(new Person("Hans", "Muster"));
personData.add(new Person("Ruth", "Mueller"));
personData.add(new Person("Heinz", "Kurz"));
personData.add(new Person("Cornelia", "Meier"));
personData.add(new Person("Werner", "Meyer"));
personData.add(new Person("Lydia", "Kunz"));
personData.add(new Person("Anna", "Best"));
personData.add(new Person("Stefan", "Meier"));
personData.add(new Person("Martin", "Mueller"));
}
就是这么个流程,欢迎大家批评指正,谢谢。