How to populate TableView from list of String[] in

2019-09-08 01:14发布

I'm trying to dynamically populate a TableView with arrays of strings taht I have inside of a list (I guess there are better ways to handle information, but its is what I was given to do...), so I create a tableView, create all the columns and stuff, but I'm lost at the moment of populating the tableview.

The list has all the string arrays inside and everything seems to work, there is no missplaced data on any array or anything like that, and when I run the project, the column names appear as I wanted them to, and there is a row on the tableview for every array on my list, but I just can't get it to display the information.

I guess I'm missing the statement to set the information from the list into the cells, but I can't see the way to do it. Any help will be thanked in its weight in cookies :0

Here's the code of the process im using so far.

int tam = regs.length-3;
String[] aux = new String[regs.length-3];
ObservableList<String[]> list = FXCollections.observableArrayList();

for (int i = 0; i < db.length; i++) {            
    aux[(i % tam)] = db[i];
    if (((i % tam) == tam-1) & (i > 0)) {
        list.add(aux);
        aux = new String[tam];
    }
}

TableView<String[]> table = new TableView<>();

TableColumn[] tableColumns = new TableColumn[tam];
for (int i = 3; i < regs.length; i++) {
    tableColumns[i-3] = new TableColumn(regs[i]);
}
table.getColumns().addAll(tableColumns);

table.setItems(list);

1条回答
不美不萌又怎样
2楼-- · 2019-09-08 01:59

You need to set cell value factories on your table columns. Something like

TableColumn[] tableColumns = new TableColumn[tam];
for (int i = 3; i < regs.length; i++) {
    tableColumns[i-3] = new TableColumn(regs[i]);
    final int index = i - 3 ; // I think, don't really understand what you're doing with indexes...
    tableColumns[i-3].setCellValueFactory(new Callback<CellDataFeatures<String[], String>>, ObservableValue<String>>) {
        @Override
        public ObservableValue<String> call(CellDataFeatures<String[], String> cellData) {
            return new ReadOnlyStringWrapper(cellData.getValue()[index]);
        }
    });
}
查看更多
登录 后发表回答