我想用从数据库查询的数据加载一个 TableView,但似乎无法让它正常工作。
单击按钮后,我会在 Netbeans 控制台中打印出结果。GUI 中的表格在运行后从“表格中无内容”更改并绘制行,但在 GUI 的表格列中未打印任何内容。
谢谢大家。这是我几天来最接近让它工作的地方。你能帮我解决这个问题吗?将项目打印到 GUI 的列中?
FXML 是通过 JavaFx SceneBuilder 完成的。
这是数据库查询类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class StudentInfo {
static String JDBC_DRIVER = "org.h2.Driver";
static String DB_URL = "jdbc:h2:file:C:/WAKILI/WAKILIdb";
// Database credentials
static final String USER = "sa";
static final String PASS = "";
public static Connection conn = null;
public List<KIWIDataModel> getAllstudentInfo() {
Statement st = null;
ResultSet rs;
String driver = "org.h2.Driver";
List ll = new LinkedList();
try {
Class.forName(driver);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
st = conn.createStatement();
String recordQuery = ("SELECT id, KIWI FROM KIWI");
rs = st.executeQuery(recordQuery);
while (rs.next()) {
int Key = rs.getInt(1);
ObservableList row = FXCollections.observableArrayList();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
row.add(rs.getString(i));
System.out.println(row);
}
KIWIDataModel roww = new KIWIDataModel();
roww.setFirstName(Key);
ll.add(row);
}
} catch (ClassNotFoundException | SQLException ex) {
// CATCH SOMETHING
}
return ll;
}
}
这是控制器类:
import DB.KIWI.Try.KIWIDataModel;
import DB.KIWI.Try.StudentInfo;
import DBQuery.DynamicTable;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class SampleController implements Initializable {
@FXML
private TableView<KIWIDataModel> Table;
@FXML
private TableColumn<KIWIDataModel, Integer> colKey;
@FXML
public void selectKIWITable() {
System.out.println("Button Pressed");
colKey.setCellValueFactory(new PropertyValueFactory<KIWIDataModel, Integer>("Key"));
Table.getItems().setAll(new StudentInfo().getAllstudentInfo());
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
这是数据模型类:
import javafx.beans.property.SimpleIntegerProperty;
public class KIWIDataModel {
public SimpleIntegerProperty firstName;
public int getFirstName() {
return firstName.get();
}
public void setFirstName(int fName) {
firstName.set(fName);
}
}
这是通过 JavaFx 场景生成器生成的 FXML 脚本:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="700.0" xmlns:fx="http://javafx.com/fxml" fx:controller="wakiliproject.SampleController">
<children>
<TableView fx:id="Table" prefHeight="400.0" prefWidth="700.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="Column X" fx:id="colKey" />
</columns>
</TableView>
</children>
</AnchorPane>