0
我想加載一個TableView與數據庫中查詢的數據,但似乎無法讓它正常工作。加載數據到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);
}
}
這是FXML腳本通過JavaFx場景生成器生成:
<?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>
嗨[Uluk Biy](http://stackoverflow.com/users/682495/uluk-biy)。我試過了你的建議,但我仍然沒有得到任何東西! – ORey
@ORey。查看更新。 –
我不知道我在做什麼錯誤[Uluk Biy](http://stackoverflow.com/users/682495/uluk-biy),但即使如此,我仍然無法使它工作。 – ORey