Reputation:
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:
Upvotes: 1
Views: 5264
Reputation: 1
columnCita.setCellValueFactory(new PropertyValueFactory<>("cita"));
columnLast.setCellValueFactory(new PropertyValueFactory<>("ultimavisita"));
columnImporteF.setCellValueFactory(new PropertyValueFactory<>("precio"));
columnCita.prefWidthProperty().bind(table.widthProperty().divide(15));
columnLast.prefWidthProperty().bind(table.widthProperty().divide(6));
columnImporteF.prefWidthProperty().bind(table.widthProperty().divide(9));
in my case the wrong is in the next line. becose already i had added the columns.
table.getColumns().addAll(columnCita, columnLast, columnImporteF);
the problem is you are adding the columns again.
Upvotes: 0
Reputation: 209235
Just clear the current columns before you start adding them:
table.getColumns().clear();
for(int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
// ...
}
Upvotes: 1