Search code examples
javamysqljavafxfxml

Tableview only shows the last input added to observableList()


Im attempting to create a tableview that shows all the rows that a specified resultset returns, I'm using observableList to store these row's which store all the rows just fine but I only get the output of the first row added. I'd like to specify I'm not currently getting any errors, my code is as follows.

public class StockController implements Initializable {

  @FXML
   private TableView tableview;

  ObservableList<ObservableList> data = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
   
    dbconn conn = new dbconn();
    
    try{
    String SQL = "SELECT * FROM recibido WHERE id < 3";
    
    ResultSet rs = conn.con.createStatement().executeQuery(SQL);
    
    for(int i = 0; i < rs.getMetaData().getColumnCount(); i++){
        final int j = i;
        TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
        col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>(){
             public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {                                                                                              
                    return new SimpleStringProperty(param.getValue().get(j).toString());                        
                }   
        });
        
        tableview.getColumns().addAll(col);
        System.out.println("Column ["+i+"] ");
        
    }
    ObservableList<String> row = FXCollections.observableArrayList();
    while(rs.next()){
        
        for(int i = 1; i<=rs.getMetaData().getColumnCount(); i++){
            row.add(rs.getString(i));
        }
        System.out.println("Row [1] added "+row);
        data.add(row);
        
    }
    
    tableview.setItems(data);
    
   
}    catch( Exception e){
    e.printStackTrace();
    System.out.println("Error on Building Data"); 
}
}
@FXML
private void goToReception(ActionEvent event) throws IOException {
    Parent reception = FXMLLoader.load(getClass().getResource("reception.fxml"));
    Scene scene = new Scene(reception);
    Stage receptionWindow = (Stage)((Node)event.getSource()).getScene().getWindow();
    
    receptionWindow.setScene(scene);
    receptionWindow.show();
}

}

I believe my problem could be related to a incorrect initiation of

ObservableList<ObservableList> data = FXCollections.observableArrayList();

Hope somebody can help me I've been going crazy for days.

Code Compiled/Output. enter image description here


Solution

  • Please change the following :

    ObservableList<String> row = FXCollections.observableArrayList();
    while(rs.next()){
        
        for(int i = 1; i<=rs.getMetaData().getColumnCount(); i++){
            row.add(rs.getString(i));
        }
        System.out.println("Row [1] added "+row);
        data.add(row);
        
    }
    
    tableview.setItems(data);
    

    to

    while(rs.next()){
       ObservableList<String> row = FXCollections.observableArrayList();     
        for(int i = 1; i<=rs.getMetaData().getColumnCount(); i++){
            row.add(rs.getString(i));
        }
        System.out.println("Row [1] added "+row);
        data.add(row);
        
    }
    
    tableview.setItems(data);