JavaFX 8: Iterate through TableView cells and get graphics

I'm having problems accessing objects displayed in a TableColumn

Here's a snippet of code where I set the graphics for one column. Just want to show the Name of Person object: (Any better / easier way to do this 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 skip rows and columns so that each cell generates 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() ?
            }
        }
    }

So the question is, how do I get getGraphics () that I installed in CellFactory?

+2
source share
3 answers

A static method for any type of cell content.

getItems() . TableView. , , 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;
  }
+2

, Cell, , CellFactory 1:1 . .

, :

@Override
protected void updateItem(Person item, boolean empty) {
    super.updateItem(item, empty);
    if (item != null && !empty) {
        setText(item.getName());
    } else {
        setText(null);
    }
}

.

a)

b) / , ( ) - !

c) super udateItem().

+1

-, 1-1 , .

You should think about table.getItems()how the data is displayed. Table columns, tables, and table cells are just a visualization of this data. Return to the actual data, not its specific visualization:

for (Site site : tableView.getItems()) {
    // owner column data:
    Person owner = site.getOwner();
    String ownerName = owner.getName();
    // ...
}
0
source

Source: https://habr.com/ru/post/1674799/


All Articles