Reputation: 341
I'd like to load a TableView with data queried from a database, but can't seem to get it to work right.
On clicking the button I get a print-out of the results in the Netbeans console. The table in the GUI changes from "No content in table" after run and draws the rows but no content gets printed in the table columns in the GUI.
Thank you all in advance. This is the closest I've been in days to getting it to work. Could you please help me solve this other part? Printing the items into the columns in the GUI?
The FXML was done via JavaFx SceneBuilder.
This is the database query class:
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;
}
}
This is the controller class:
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
}
}
This is the data model class:
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);
}
}
This is the FXML script generated via JavaFx scene builder:
<?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>
Upvotes: 0
Views: 2498
Reputation: 49195
With the line
colKey.setCellValueFactory(new PropertyValueFactory<KIWIDataModel, Integer>("Key"));
JavaFX will look for KIWIDataModel.getKey() and KIWIDataModel.setKey(...) and optionally KIWIDataModel.KeyProperty() methods. Since you haven't them in mentioned class, change it as
colKey.setCellValueFactory(new PropertyValueFactory<KIWIDataModel, Integer>("firstName"));
EDIT: Also the selectKIWITable()
method is not called from anywhere. Do
@Override
public void initialize(URL url, ResourceBundle rb) {
selectKIWITable();
}
and remove @FXML from selectKIWITable()
method.
Upvotes: 2