How do I define my table in FXML and then use my JavaFX code to populate it dynamically at runtime?
Define the controller class. Few things to note:
a class Data Model class MyDataModel is used to populate the data.
public class MyController implements Initializable {
@FXML
private TableView myTableView;
@FXML
private TableColumn idColumn;
@Override
public void initialize(URL location, ResourceBundle resources) {
idColumn.setCellValueFactory(new PropertyValueFactory"idColumn"));
myTableView.getItems().setAll(getItemsToAdd());
}
private List getItemsToAdd(){
// this method would fetch the necessary items from database.
}
}
Define the Data Model class. Few things to note:
the model class must have the methods getIdColumn() and setIdColumn(String id)
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);
}
}