[英]JavaFX 8 : Iterate through TableView cells and get graphics
I'm having troubles accessing the objects displayed in the TableColumn 我在访问TableColumn中显示的对象时遇到麻烦
Here's the code snippet where I set the graphics of one Column. 这是我设置一栏图形的代码段。 Only want to show the Name of the Person object: (Any better/easier way in doing that is welcome)
只想显示“人”对象的名称:(欢迎使用任何更好/更简便的方法)
ownerColumn
.setCellFactory(new Callback<TableColumn<Site, Person>, TableCell<Site, Person>>() {
@Override
public TableCell<Site, Person> call(
TableColumn<Site, Person> param) {
TableCell<Site, Person> ownerCell = new TableCell<Site, Person>() {
@Override
protected void updateItem(Person item, boolean empty) {
if (item != null) {
Label label = new Label(item.getName());
setGraphic(label);
}
}
};
return ownerCell;
}
});
Now I'm trying to loop through the rows and columns go get every cell to generate a report at the end, reflecting the displayed text/graphics in the Tableview. 现在,我尝试遍历行和列,让每个单元格最后生成一个报告,以在Tableview中反映显示的文本/图形。 Like this
像这样
for (Object r : this.tableview.getItems()) {
for (Object c : this.tableview.getColumns()) {
javafx.scene.control.TableColumn column = (javafx.scene.control.TableColumn) c;
if (column.getCellData(r) != null) {
// I get the object bound to the cell here, but I only want
// to have what i've set as graphics - what the user sees on UI
// How to use getGraphics() ?
}
}
}
So the question is, how do I get the getGraphics() I've set in the CellFactory? 所以问题是,如何获取在CellFactory中设置的getGraphics()?
A static method for any type of cell content. 任何类型的单元格内容的静态方法。
Method getItems() gets the value of the property items.
方法getItems()获取属性项的值。 Property is the underlying data model for the TableView.
属性是TableView的基础数据模型。 Note that it has a generic type that must match the type of the TableView itself.
请注意,它具有必须与TableView本身类型匹配的通用类型。
private static ArrayList<String> getTableViewValues(TableView tableView) {
ArrayList<String> values = new ArrayList<>();
ObservableList<TableColumn> columns = tableView.getColumns();
for (Object row : tableView.getItems()) {
for (TableColumn column : columns) {
values.add(
(String) column.
getCellObservableValue(row).
getValue());
}
}
return values;
}
This approach, accessing the Cell, will not work, as the CellFactory will not be used to produce a 1:1 Cell for each row. 这种访问单元的方法将不起作用,因为CellFactory不会用于为每一行生成1:1单元。 The Cell is only used as a lightweight View generator and will be reused heavily.
该单元仅用作轻量级View生成器,并将大量重复使用。
By the way you might use: 顺便说一句,您可以使用:
@Override
protected void updateItem(Person item, boolean empty) {
super.updateItem(item, empty);
if (item != null && !empty) {
setText(item.getName());
} else {
setText(null);
}
}
will simply show a text. 只会显示一个文本。
a) Check also the boolean empty a)还检查布尔值是否为空
b) Explicitely clear all graphic/text that will be set (test here) due to the reuse cases! b)由于重复使用情况,请明确清除将要设置的所有图形/文本(在此处进行测试)!
c) Always call the super udateItem() method. c)始终调用super udateItem()方法。
As Jens-Peter says, there isn't a 1-1 correspondence between cells and items in the table, so the approach of getting the cell will not work. 正如詹斯·彼得(Jens-Peter)所说,表格中的单元格与项目之间没有1-1的对应关系,因此获取单元格的方法将行不通。
You should think of table.getItems()
as having the data that's displayed. 您应该认为
table.getItems()
具有显示的数据。 The table, table columns, and table cells are just visualizations of those data. 表,表列和表单元格只是这些数据的可视化。 Refer back to the actual data, not to the specific visualization of it:
请参考实际数据,而不是具体的可视化:
for (Site site : tableView.getItems()) {
// owner column data:
Person owner = site.getOwner();
String ownerName = owner.getName();
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.