While i was searching for adding rows from DB to the table view in javaFX, i came across below code from the following code snippet.
Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>()
I cannot Understand what it does in this code. i understood that setCellValueFactory is for adding value for the cell but i cannot understand this series of code from callback
public class DBFXLesson1 extends Application {
private ObservableList<ObservableList> data;
private TableView tableview;
public static void main(String args[]) {
Application.launch(args);
}
//CONNECTION DATABASE
public void buildData() {
Connection conn;
conn = new DBConnection().getConnection();
data = FXCollections.observableArrayList();
try {
//SQL FOR SELECTING ALL OF CUSTOMER
String SQL = "SELECT * from addrbk";
//ResultSet
ResultSet rs = conn.createStatement().executeQuery(SQL);
/**
* ********************************
* TABLE COLUMN ADDED DYNAMICALLY * ********************************
*/
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
//We are using non property style for making dynamic table
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1));
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
tableview.getColumns().addAll(col);
System.out.println("Column [" + i + "] ");
}
/**
* ******************************
* Data added to ObservableList * ******************************
*/
while (rs.next()) {
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
//Iterate Column
row.add(rs.getString(i));
}
System.out.println("Row [1] added " + row);
data.add(row);
}
//FINALLY ADDED TO TableView
tableview.setItems(data);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
public void start(Stage stage) {
//TableView
tableview = new TableView();
buildData();
//Main Scene
Scene scene = new Scene(tableview);
stage.setScene(scene);
stage.show();
}
}
My Question is
what is the purpose of this callback and the flow of the code.
When and Where the rows get inserted to the table View