JavaFX 8:迭代TableView单元格并获取图形 [英] JavaFX 8 : Iterate through TableView cells and get graphics

查看:557
本文介绍了JavaFX 8:迭代TableView单元格并获取图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法访问TableColumn中显示的对象

I'm having troubles accessing the objects displayed in the TableColumn

这是我设置一列图形的代码片段。只想显示Person对象的名称:(欢迎使用任何更好/更简单的方法)

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

现在我正在尝试遍历行和列,让每个单元格生成报告最后,反映Tableview中显示的文本/图形。像这样

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. 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() ?
            }
        }
    }

所以问题是,如何我是否在CellFactory中设置了getGraphics()?

So the question is, how do I get the getGraphics() I've set in the CellFactory?

推荐答案

任何类型的单元格内容的静态方法。

A static method for any type of cell content.


方法getItems()获取属性项的值。属性是
TableView的基础数据模型。请注意,它具有
泛型类型,必须与TableView本身的类型匹配。

Method getItems() gets the value of the property items. Property is the underlying data model for the TableView. Note that it has a generic type that must match the type of the TableView itself.



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

这篇关于JavaFX 8:迭代TableView单元格并获取图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆