我开始学习JavaFX,我想用我的数据库中的数据填充我的tableview。我已经了解到我可以使用地图来构建数据,但它并没有起作用。 tableview只显示列'名称但没有行。
这是我的代码:
for (let i = 0; i < 100; i++) {
setTimeout(() => console.log("test"), 1000 * i);
}
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package application;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
public class MainWindowController {
@FXML
private TableView<Map<Integer, String>> tableView = new TableView<>(generateDataInMap());
final static Integer first = 1;
public void openDatabase() throws SQLException {
Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db");
Statement statement = connection.createStatement();
ResultSet rSet = statement.executeQuery("select * from myTable");
int col = rSet.getMetaData().getColumnCount();
for (int i = 1; i <= col; i++) {
rSet = statement.executeQuery("select * from myTable");
Integer key = 1;
String name = rSet.getMetaData().getColumnName(i);
TableColumn<Map<Integer, String>, String> tableColumn = new TableColumn<>(name);
tableColumn.setCellValueFactory(new MapValueFactory(key));
tableView.getColumns().add(tableColumn);
}
}
private ObservableList<Map<Integer, String>> generateDataInMap(){
Connection connection = null;
ObservableList<Map<Integer, String>> alldata = FXCollections.observableArrayList();
try {
connection = DriverManager.getConnection("jdbc:sqlite:test.db");
Statement statement = connection.createStatement();
ResultSet rSet = statement.executeQuery("select * from myTable");
int col = rSet.getMetaData().getColumnCount();
while(rSet.next()) {
Map<Integer, String> dataRow = new HashMap<>();
for (int i = 1; i <= col; i++) {
String value = rSet.getString(i);
dataRow.put(i, value);
}
alldata.add(dataRow);
}
connection.close();
return alldata;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
您知道我的代码中的错误在哪里吗?我试图模拟SQLiteStudio的功能,当我选择数据库时,数据将填充tableview。
答案 0 :(得分:0)
您正在控制器类的初始值设定项中创建TableView
。此TableView
填充了数据,但实例永远不会显示在场景中,因为FXMLLoader
会从fxml创建一个实例。
在initialize()
方法中进行这样的初始化。注入所有对象后,此方法由FXMLLoader
运行,使屏幕上显示的tableView
实例可访问。
key
的{{1}}传递给值工厂。相反,您应该使用1
。i
请注意,上面修改过的代码版本要求您删除@FXML
private TableView<Map<Integer, String>> tableView;
@FXML
private void initialize() throws Exception {
try (Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db");
Statement statement = connection.createStatement()) {
ResultSet rSet = statement.executeQuery("select * from myTable");
openDatabase(rSet);
tableView.setItems(generateDataInMap(rSet));
}
// TODO: Exception handling???
}
private void openDatabase(ResultSet rSet) throws SQLException {
final ResultSetMetaData metadata = rSet.getMetaData();
final int col = metadata.getColumnCount();
for (int i = 1; i <= col; i++) {
String name = metadata.getColumnName(i);
TableColumn<Map<Integer, String>, String> tableColumn = new TableColumn<>(name);
tableColumn.setCellValueFactory(new MapValueFactory(i));
tableView.getColumns().add(tableColumn);
}
}
private ObservableList<Map<Integer, String>> generateDataInMap(ResultSet rSet) {
ObservableList<Map<Integer, String>> alldata = FXCollections.observableArrayList();
final int col = rSet.getMetaData().getColumnCount();
while(rSet.next()) {
Map<Integer, String> dataRow = new HashMap<>();
for (int i = 1; i <= col; i++) {
String value = rSet.getString(i);
dataRow.put(i, value);
}
alldata.add(dataRow);
}
return alldata;
}
事件处理程序,但由于添加行但未初始化列没有意义,因此我将调用移至onAction
方法并修改了签名的方式不允许您再将其用作initialize
。