0

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 Answer 1

1

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]);
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.