如何将行和列添加到 JavaFX 8 TableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25395016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add rows and columns to a JavaFX 8 TableView
提问by Victor Grazi
I see examples on the internet for adding a row to a TableView, for example using the Person class in the Oracle documentation.
我在 Internet 上看到将行添加到 TableView 的示例,例如使用Oracle 文档中的 Person 类。
But I have a variable number of columns, so I can't bind to a Person (or any other) bean business object.
但是我有可变数量的列,所以我无法绑定到 Person(或任何其他)bean 业务对象。
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.
Oracle 示例继续展示如何将列绑定到属性名称,但为此,它只展示了如何添加列,而不展示如何添加行。
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?
我的问题是,有人能给我指出一个 Hello, World 动态添加任意列和/或行到 JavaFX 8 TableView 的例子吗?
采纳答案by James_D
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.
使用List<String>
(例如)作为数据类型,并将单元格值工厂设置为索引到列表中的回调。
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):
例如,这将创建一个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 ;
}
回答by Victor Grazi
Kind of clunky, but this sample code seems to work:
有点笨重,但这个示例代码似乎有效:
TableView table = new TableView<>();
private char nextChar = 'A';
private void addColumn(TableView table) {
String mapChar = String.valueOf(nextChar++);
TableColumn<Map, String> column = new TableColumn<>("Class " + mapChar);
column.setCellValueFactory(new MapValueFactory(mapChar));
column.setMinWidth(130);
column.setCellFactory(cellFactoryForMap);
table.getColumns().add(column);
}
private void addRow(TableView table) {
ObservableList<Map> allData = table.getItems();
int offset = allData.size();
Map<String, String> dataRow = new HashMap<>();
for (int j = 0; j < table.getColumns().size(); j++) {
String mapKey = Character.toString((char) ('A' + j));
String value1 = mapKey + (offset + 1);
dataRow.put(mapKey, value1);
}
allData.add(dataRow);
}
}