0

Hello i have an issue i am trying to display data from database into a TableView using Java because i couldn't find in JavaFx anything like a DataGridView in C#. I made the following method to fill the table:

private void fillTableView() throws SQLException {
        // fill header      
        for (int i = 2; i <= resultSet.getMetaData().getColumnCount(); i++) {
            TableColumn column = new TableColumn(resultSet.getMetaData().getColumnName(i));
            tableView.getColumns().addAll(column);
        }       

        // fill rows
        while (resultSet.next()) { // loop through the rows by one
            ObservableList<String> row = FXCollections.observableArrayList();
            for (int i = 2; i <= resultSet.getMetaData().getColumnCount(); i++) { // loop through the cells of each row    
                // add the value of this cell to the valueToAdd string
                row.add(resultSet.getString(i));
            }
            tableView.getItems().add((row));
            //rows.add(rows);
        }
        //tableView.setItems(rows);
    }

The first part works good and the column names are visible however, the rows of data appear empty yet they could be selected, so i can select the first 5 rows in the TableView ( I have five rows in my ResultSet) but those rows appear empty. I tried to remove this part

tableView.getItems().add((row));

And instead un-comment the two lines of code below it but still it didn't work. I tried to make objects initialized with the values from the database then add the objects to the TableView rather than adding an ObservableList of strings representing the rows as follows but still gave the same result (empty rows)

while (resultSet.next()) {
            Employee employee = new Employee(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4));
            tableView.getItems().add(employee);
        }

Can someone tell me what am i doing wrong please ?

I am using Netbeans 8, Scene builder, Java 8 and JavaDB (aka Apache Derby).

prasad_
  • 12,755
  • 2
  • 24
  • 36
Zeyad
  • 537
  • 2
  • 7
  • 15
  • Try to use `tableView.setItems(row);` instead of `tableView.getItems().add((row));` Remember that the must match the `row/ObservableList` type must be the same as the type of the TableView. – sorifiend Oct 24 '18 at 01:41
  • Here is a link to a query similar to yours: [How to fill up a TableView with database data](https://stackoverflow.com/questions/18941093/how-to-fill-up-a-tableview-with-database-data). – prasad_ Oct 24 '18 at 02:28
  • You could also try searching for "javafx populate tableview from database" - there are issues and examples for reference. – prasad_ Oct 24 '18 at 02:38
  • It is ideal to separate the code that reads from the database and the code that populates/displays the data into he tableview GUI. Try this: (1) Read the resultset data into the `ObservableList` collection _completely_ and after you exit the while loop, try this next (2) `table.setItems(listData);`. – prasad_ Oct 24 '18 at 02:52
  • your columns have no cellValueFactory – kleopatra Oct 24 '18 at 08:23
  • sorifiend: I tried this, it is the commented part in the bottom of the code, but didn't work either prasad_: i tried this also, in the first trial in the code i loaded my data completely into a result set, then put it into an ObservableList but it gave the same result so i thought i can couple them together but still same issue, but good point kleopatra: can you further explain more about the cellValueFactory and why would i need to set that in my case, because it's my first time to use TableView – Zeyad Oct 24 '18 at 12:44

0 Answers0