Use a List<String>
(for example) for the data type, and just set the cell value factory as a callback that indexes into the list.
For example, this will create a TableView<List<String>>
that is constructed out of an arbitrary tab-delimited text file. Not all rows in the file need have the same number of elements (it will pad with blanks). (It doesn’t support escaped tabs, etc):
public TableView<List<String>> readTabDelimitedFileIntoTable(Path file) throws IOException {
TableView<List<String>> table = new TableView<>();
Files.lines(file).map(line -> line.split("\t")).forEach(values -> {
// Add extra columns if necessary:
for (int i = table.getColumns().size(); i < values.length; i++) {
TableColumn<List<String>, String> col = new TableColumn<>("Column "+(i+1));
col.setMinWidth(80);
final int colIndex = i ;
col.setCellValueFactory(data -> {
List<String> rowValues = data.getValue();
String cellValue ;
if (colIndex < rowValues.size()) {
cellValue = rowValues.get(colIndex);
} else {
cellValue = "" ;
}
return new ReadOnlyStringWrapper(cellValue);
});
table.getColumns().add(col);
}
// add row:
table.getItems().add(Arrays.asList(values));
});
return table ;
}