Duplicate columns in JavaFX TableView

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()
  • 在这里输入图像描述在这里输入图像描述


    在开始添加它们之前,只需清除当前列:

    table.getColumns().clear();
    for(int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
        // ...
    }
    
    链接地址: http://www.djcxy.com/p/80144.html

    上一篇: JavaFX TableView:在特定列中添加各个值

    下一篇: JavaFX TableView中的重复列