简体   繁体   English

JavaFX 8:遍历TableView单元并获取图形

[英]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.

相关问题 openpyxl遍历列=&gt; TypeError的单元格:&#39;generator&#39;对象不可下标 - openpyxl iterate through cells of column => TypeError: 'generator' object is not subscriptable 使用vba迭代Excel中的所有文本(单元格,图表,文本框) - Iterate through all text (in cells, charts, textboxes) in Excel using vba 遍历行,将某些单元格中的值复制到另一个工作簿 - Iterate through row, copy value from certain cells to another Workbook VBA函数迭代单元格,用相对的列标题值替换单元格 - VBA function to iterate through cells, replacing a cell with the relative column header value 如何遍历 NumPy 数组,同时按索引过滤单元格值并对其执行数学运算 - How to iterate through a NumPy array while filtering cells values by index and performing math on it 遍历子对象并使用 javascript 获取所有值 - Iterate through the child objects and get all the values with javascript 我如何遍历此列表以获得正确的答案? - How do I iterate through this list to get the right answer? 遍历html表并获取javascript中每一行的行号 - iterate through a html table and get row number for each row in javascript 如何使用遍历表并获取每个复选框值 - How to iterate through table with and get each checkbox value 如何获得循环遍历列表的方式? - How can I get my loop to iterate through my list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM