I need to populate a TableView (JavaFx) with database items. I designed the TableView in JavaFx Scene Builder.
So far the TableView does not change at all and only remains blank.
This is what I have so far, but have been failing to get anything into the TableView. I will really appreciate any help; my second day now at this.
The classes:
This is the Class that creates the database data object:
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity(name = "KIWI_TABLE")
public class NewBeautifulKiwi implements Serializable {
@Id
@GeneratedValue
private int KiwiId;
private String Kiwi;
public int getKiwiId() {
return KiwiId;
}
public void setKiwiId(int KiwiId) {
this.KiwiId = KiwiId;
}
public String getKiwi() {
return Kiwi;
}
public void setKiwi(String Kiwi) {
this.Kiwi = Kiwi;
}
}
The Controller class:
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import tableviewfix.Hibernate.HibernateTransaction;
import tableviewfix.HibernatePersist.PersistNewBeautifulKiwi;
public class FXMLDocumentController implements Initializable {
@FXML
private TableView<NewBeautifulKiwi> KIWI_TABLE;
@FXML
private TableColumn<NewBeautifulKiwi, Integer> kiwiid;
@FXML
private TableColumn<NewBeautifulKiwi, String> kiwi;
public ObservableList<NewBeautifulKiwi> data;
private List<NewBeautifulKiwi> initializeTable() {
data = FXCollections.observableArrayList();
HibernateTransaction hibernateTransaction = new HibernateTransaction();
Iterator ite = hibernateTransaction.hibernateQuerry().list().iterator();
while (ite.hasNext()) {
NewBeautifulKiwi obj = (NewBeautifulKiwi) ite.next();
data.add(obj);
}
return data;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
kiwiid.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, Integer>("kiwiid"));
kiwi.setCellValueFactory(new PropertyValueFactory<NewBeautifulKiwi, String>("kiwi"));
KIWI_TABLE.getItems().setAll(initializeTable());
}
}
The FXML file:
<?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="293.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="tableviewfix.FXMLDocumentController">
<children>
<TableView id="showKiwiHomeTable" fx:id="KIWI_TABLE" prefHeight="293.0" prefWidth="320.0" tableMenuButtonVisible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="kiwiid" fx:id="kiwiid" />
<TableColumn prefWidth="75.0" text="kiwi" fx:id="recentKiwiShow" />
</columns>
</TableView>
</children>
</AnchorPane>
The hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:file:C:/WAKILI/WAKILIdb</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Names the annotated entity class -->
<mapping class="tableviewfix.NewBeautifulKiwi"/>
</session-factory>
</hibernate-configuration>