在 JavaFX 中从 mysql db 添加动态数据时回调的用途是什么
What is the use of Callback while adding dynamic data from mysql db in JavaFX
当我在 javaFX 中搜索将行从数据库添加到 table 视图时,我发现了以下代码片段中的代码。
Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>()
我无法理解它在这段代码中的作用。
我了解到 setCellValueFactory 用于为单元格添加值
但是我无法理解 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();
}
}
我的问题是
what is the purpose of this callback
and the flow of the code.
When and Where the rows get inserted to the table View
包含 Callback
的 for
循环只是配置列:它没有添加任何数据。 Callback
是一个函数,它从特定行中获取数据(在本例中以 ObservableList
的形式)和 returns 正在配置的列中该行的值:所以在此示例中,列 j
是对 ObservableList
(行数据)调用 get(j)
,对结果调用 toString()
,并将其包装在 SimpleStringProperty
中。
第二个循环(while
循环)实际上用数据填充 table。它(在这个例子中)通过为每一行创建一个 ObservableList
然后将每个 ObservableList
添加到一个名为 data
的 ObservableList<ObservableList>
*(即列表的列表)来做到这一点。然后在最后它调用 tableView.setItems(data)
告诉 TableView
使用它作为它的数据列表。
这段代码写得不太好:它使用了很多原始类型,您的编译器应该会从中剔除一大堆警告。
通常,只有在高度动态的情况下(即数据库 table 中有一组未知的列),您才会使用此结构。通常,当数据库 table 模式固定时,您将为每一行创建一个特定的模型对象(而不是使用通用的 List
)并为其定义属性。 This example 使用这种更常见的方法。即使您有动态案例,我也强烈建议不要对数据结构使用原始类型。
放:
return new SimpleStringProperty((String) param.getValue().get(j));
代替
return new SimpleStringProperty(param.getValue().get(j).toString());
当我在 javaFX 中搜索将行从数据库添加到 table 视图时,我发现了以下代码片段中的代码。
Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>()
我无法理解它在这段代码中的作用。 我了解到 setCellValueFactory 用于为单元格添加值 但是我无法理解 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();
}
}
我的问题是
what is the purpose of this callback and the flow of the code.
When and Where the rows get inserted to the table View
包含 Callback
的 for
循环只是配置列:它没有添加任何数据。 Callback
是一个函数,它从特定行中获取数据(在本例中以 ObservableList
的形式)和 returns 正在配置的列中该行的值:所以在此示例中,列 j
是对 ObservableList
(行数据)调用 get(j)
,对结果调用 toString()
,并将其包装在 SimpleStringProperty
中。
第二个循环(while
循环)实际上用数据填充 table。它(在这个例子中)通过为每一行创建一个 ObservableList
然后将每个 ObservableList
添加到一个名为 data
的 ObservableList<ObservableList>
*(即列表的列表)来做到这一点。然后在最后它调用 tableView.setItems(data)
告诉 TableView
使用它作为它的数据列表。
这段代码写得不太好:它使用了很多原始类型,您的编译器应该会从中剔除一大堆警告。
通常,只有在高度动态的情况下(即数据库 table 中有一组未知的列),您才会使用此结构。通常,当数据库 table 模式固定时,您将为每一行创建一个特定的模型对象(而不是使用通用的 List
)并为其定义属性。 This example 使用这种更常见的方法。即使您有动态案例,我也强烈建议不要对数据结构使用原始类型。
放: return new SimpleStringProperty((String) param.getValue().get(j)); 代替 return new SimpleStringProperty(param.getValue().get(j).toString());