I am a newbie in here, my Table View in JavFX namely as tblStudent is NOT containing the
records that I want. How to do this, please help me.
public void populateData(){
Connection c ;
data = FXCollections.observableArrayList();
try{
c = ConnectionClass.connect();
//SQL FOR SELECTING ALL OF CUSTOMER
String SQL = "SELECT * FROM `student`";
//ResultSet
ResultSet rs = c.createStatement().executeQuery(SQL);
studentIDCol = new TableColumn("Student ID");
nameCol = new TableColumn("Name");
addressCol = new TableColumn("Address");
ageCol = new TableColumn("Age");
contactnoCol = new TableColumn("Contact Number");
while(rs.next()){
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
row.add(rs.getString("id"));
row.add(rs.getString("name"));
row.add(rs.getString("address"));
row.add(rs.getString("age"));
row.add(rs.getString("contact_num"));
System.out.println("Row [1] added "+row );
data.add(row);
}
//FINALLY ADDED TO TableView
tblStudent.getItems().setAll(data);
}catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
Anyone who knows this?
You need to set the cell value factory to all your columns like this:
And also I recommend you to extract your query to a service, because the query will block the JavaFX Thread. Check out http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm
Check this full example with test data.
If you are using a FXML file. You need to skip the initialization step of the TableColumn.
Declare the TableColumn as a fields in the file in your controller class with the @FXML annotation. Ensure you implements javafx.fxml.Initializable. And in the initialize method you can do the configuration side for every column. And avoid the add of the columns to the table, because they already are in the table view.
Here is a sample if your are defining your TableView in your FXML like this:
Then your controller class will be like this:
Hope it helps.