1

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.

Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
Hafiz Atif
  • 19
  • 5
  • 1
    You need cell value factories. See this tutorial: https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm – Itai May 09 '16 at 07:45
  • i followed the steps given by http://stackoverflow.com/questions/32808905/javafx-dynamic-tableview-is-not-showing-data-from-the-database but still could not find the error. – Hafiz Atif May 09 '16 at 16:03

1 Answers1

1

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));
fabian
  • 80,457
  • 12
  • 86
  • 114