Hafiz Atif
Hafiz Atif

Reputation: 19

could not be able to load data into javafx tabels

need help... here is the output of table view i could not be able to find why it's not displaying any row's data except columns.I mean it displays only database columns Here is the code

 @Override
 public void start(Stage stage) throws Exception {
     TableView<ObservableList<String>> tableview = new TableView();
     ObservableList<ObservableList<String>> data = FXCollections.observableArrayList();
     Connection c = DBConnect.connect(); 
     String sql = "SELECT * FROM INVENTORY.CUSTOMER";
     ResultSet rs = c.createStatement().executeQuery(sql);
     ResultSetMetaData metaData = rs.getMetaData();
     int count = metaData.getColumnCount();
     for (int i = 0; i < count; i++){
         System.out.println(metaData.getColumnLabel(i + 1));
         TableColumn<ObservableList<String>, String> col = new TableColumn<>(metaData.getColumnLabel(i + 1));
         tableview.getColumns().add(col);

     }
     while(rs.next()) {
         ObservableList<String> row = FXCollections.observableArrayList();

         for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
             row.add(rs.getString(i));
         }
         data.add(row);
         System.out.println("Row [1] added " + row);
     }
     tableview.setItems(data);
     rs.close();

     Scene scene = new Scene(tableview);        
     stage.setScene(scene);
     stage.show();
 }

Hope you people will find the wrong part. I am not getting any kind of error too.

Upvotes: 1

Views: 71

Answers (1)

fabian
fabian

Reputation: 82531

You need to specify a cellValueFactory. Otherwise the TableView doesn't know which part of the item to display in a specific column:

TableColumn<ObservableList<String>, String> col = new TableColumn<>(metaData.getColumnLabel(i + 1));
final int index = i; // is this the index corresponding to the list element to display???
col.setCellValueFactory(cellData -> Bindings.stringValueAt(cellData.getValue(), index));

Upvotes: 1

Related Questions