Duplicate columns in JavaFX TableView

user710031

I'm pulling a ResultSet from a MySQL database and attempting to dynamically build a TableView on a key down event. It works fine the first time I trigger it, but all subsequent times it creates duplicates of my initial columns while changing the row data for each column to match the new ResultSet. I want the TableView to be cleared completely every time this build function is run.

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<TableColumn.CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){                    
                @Override
                public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {                                                                                              
                    return new SimpleStringProperty(param.getValue().get(j).toString());                        
                }                    
            });

            tableView.getColumns().addAll(col);
            System.out.println("Column ["+i+"] ");
        }

        while(rs.next())
        {
            ObservableList<String> row = FXCollections.observableArrayList();
            row.removeAll(row);
            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);

Things I tried:

  • Watching the value of ObservableList data, which is empty as it is reinstantiated everytime the function is executed
  • Clearing the columns of the TableView using tableView.getColumns.removeAll()

enter image description here enter image description here

James_D

Just clear the current columns before you start adding them:

table.getColumns().clear();
for(int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
    // ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related