I've created a TableView using Scene Builder as follows-
Here is the FXML code-
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="508.0" prefWidth="483.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="imran.jfx.application.Controller">
<children>
<TableView fx:id="tableview" layoutX="19.0" layoutY="94.0" prefHeight="402.0" prefWidth="443.0">
<columns>
<TableColumn fx:id="id" prefWidth="75.0" text="ID" />
<TableColumn fx:id="english" prefWidth="170.0" text="English" />
<TableColumn fx:id="bangla" prefWidth="197.0" text="Bangla" />
</columns>
</TableView>
<Button fx:id="showTableview" layoutX="188.0" layoutY="52.0" mnemonicParsing="false" onAction="#showTableData" text="Show Tables" />
</children>
</Pane>
I tried to connect with the database and tried to show data from the database to tableview. I tried to implement this things into Controller Class as follows-
package imran.jfx.application;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.util.Callback;
public class Controller implements Initializable{
private ObservableList<ObservableList> data;
@FXML
private TableView<?> tableview;
@FXML
private TableColumn<?, ?> id;
@FXML
private TableColumn<?, ?> english;
@FXML
private TableColumn<?, ?> bangla;
@FXML
private Button showTableview;
ResultSet result;
PreparedStatement doQuery;
Connection conn;
String query;
@Override
public void initialize(URL location, ResourceBundle resources)
{
try
{
Class.forName("org.sqlite.JDBC");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url="jdbc:sqlite::resource:database/bn_words.db";
try
{
conn = DriverManager.getConnection(url);
}
catch (SQLException e) {
e.printStackTrace();
}
}
public void buildData()
{
data = FXCollections.observableArrayList();
try{
Class.forName("org.sqlite.JDBC");
String url="jdbc:sqlite::resource:database/bn_words.db";
conn = DriverManager.getConnection(url);
String SQL = "SELECT _id,en_word,bn_word from words";
ResultSet rs = conn.createStatement().executeQuery(SQL);
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++)
{
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
tableview.getColumns().addAll(col);
System.out.println("Column ["+i+"] ");
}
while(rs.next()){
ObservableList<String> row = FXCollections.observableArrayList();
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
row.add(rs.getString(i));
}
System.out.println("Row [1] added "+row );
data.add(row);
}
tableview.setItems(data);
}catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
@FXML
void showTableData(ActionEvent event) {
tableview = new TableView();
buildData();
}
}
The problem is, it is showing error for the line tableview.setItems(data);
. I don't know the reason for this error. If I edit the line private TableView<?> tableview;
to private TableView tableview;
then it doesn't shows any error for the line tableview.setItems(data);
. Then the app runs well. But when I click on the button Show Tables, it doesn't show the data into the TableView, but it shows the data into console.
There also shown lots of warnings as follows-
Should I ignore these errors? How do I fix my problem?