I hit a wall when i retrieve data from database.I managed to get column names into my table which is ok and works as it shoud.
Bud when i try to add rows i find that no data has been added into my tableview,im trying to add rows from array of string which represent single row.I have no idea how many columns etc will user ask for with his query and how big table is so i cant create row object and all the properties which represent each column.
Question is how do i populate my tableview with rows that i have retrieved successfully from database in format of String[] each row.
private ObservableList<String[]> dataRows=FXCollections.observableArrayList();
Retrieve rows..(Works debug prints all data correct)
while (this.resultSet.next()) {
for (int i = 1; i < this.resultSetMd.getColumnCount() + 1; i++) {
rows.add(resultSet.getString(i));
}
dataRows.add(rows.toArray(new String[rows.size()]));
rows.removeAll(rows);
//TODO DELETE row debugger debug
dataRows.forEach((r)->{
System.out.println("Row---------");
for(String s:r){
System.out.println("Debug row value- "+s);
}
});
Adding into table
private void populateTable() {
dpcontroller.tableView.getColumns().addAll(columns); //columns work
dpcontroller.tableView.setItems(dataRows); //no data entered
}
EDIT- WORKING CODE AFTER ANSWER
Working code for people who will encounter same problem as i did.Only needed factory was really ValueFactory.Here are my two methods
private void populateTable() {
for (int rowNumber = 0; rowNumber < dataRows.size(); rowNumber++) {
for (TableColumn tc : columns) {
tc.setCellValueFactory(new ValueFactory(columns.indexOf(tc),rowNumber));
dpcontroller.tableView.getColumns().add(tc);
}
}
dpcontroller.tableView.setItems(dataRows);
}
private class ValueFactory implements Callback {
private int rowNumber;
private int columnNumber;
public ValueFactory(int columnNumber, int rowNumber) {
this.columnNumber = columnNumber;
this.rowNumber = rowNumber;
}
@Override
public Object call(Object o) {
SimpleStringProperty stringProperty = new SimpleStringProperty(dataRows.get(rowNumber)[columnNumber]);
return stringProperty;
}
}