使用
List<String>(例如)作为数据类型,只需将单元格值工厂设置为索引到列表的回调。
例如,这将创建一个
TableView<List<String>>由任意制表符分隔的文本文件构造的。并非文件中的所有行都需要具有相同数量的元素(它将用空格填充)。(它不支持转义的制表符等):
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 ;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)