How can I add rows and columns to a JavaFX 8 TableView

Victor Grazi picture Victor Grazi · Aug 20, 2014 · Viewed 30k times · Source

I see examples on the internet for adding a row to a TableView, for example using the Person class in the Oracle documentation.

But I have a variable number of columns, so I can't bind to a Person (or any other) bean business object.

The Oracle example goes on to show how to bind columns to property names, but for that, it only shows how to add columns, but not rows.

My question is, can someone point me to a Hello, World example of dynamically adding arbitrary columns and/or rows to a JavaFX 8 TableView?

Answer

James_D picture James_D · Aug 20, 2014

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 ;
}