0

I created a TableTree that contains object of class Component that has a boolean property "selected". I want to hide the rows from the table where the rows component is not selected. I tried this:

componentTree.setRowFactory(new Callback<TreeTableView<Component>, TreeTableRow<Component>>() {
    @Override
    public TreeTableRow<Component> call(TreeTableView<Component> param) {
        TreeTableRow<Component> row = new TreeTableRow<Component>() {
            @Override
            protected void updateItem(Component component, boolean empty) {
                if(!empty) {
                    if (!component.isSelected()) {
                        setVisible(false);
                        setManaged(false);
                        System.out.println("hide");
                    } else {
                        setVisible(true);
                        setManaged(true);
                        System.out.println("show");
                    }
                }
            }
        };
        return row;
    }
});

On system.out I can see a lot of "show" and "hide" messages, but this doesn't affect the table, all rows are shown as before.

Any idea on this topic?

Thanks!

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Levente
  • 161
  • 1
  • 7
  • Addition: I can manage the style of the row (like background color) with this code, but visibility is still not working. – Levente Feb 27 '20 at 14:07
  • 1
    filter the data to not contain selected rows – kleopatra Feb 27 '20 at 14:12
  • Regarding filtering the data displayed by `TreeTable` (as suggested in @kleopatra comment), have you seen this SO question? [How to implement filtering for treetableview](https://stackoverflow.com/questions/26072510/how-to-implement-filtering-for-treetableview) – Abra Feb 27 '20 at 14:46
  • Filtering solved the issue, thanks for the help :) – Levente Feb 28 '20 at 05:30

1 Answers1

0

I used eclipse's fx.ui.controls library for the same achieve the same goal before.

<dependency>
    <groupId>at.bestsolution.eclipse</groupId>
    <artifactId>org.eclipse.fx.ui.controls</artifactId>
    <version>2.2.0</version>
</dependency>

The library provides a class: FilterableTreeItem<T> under the tree package. This class was designed to be used in cases like yours. You can provide a Predicate to the root of the tree and the items will get hidden when the value given changes:

// Children
final FilterableTreeItem<Component> childNode1 = new FilterableTreeItem<>(component1);
final FilterableTreeItem<Component> childNode2 = new FilterableTreeItem<>(component2);
final FilterableTreeItem<Component> childNode3 = new FilterableTreeItem<>(component3);

// Root
final FilterableTreeItem<Component> root = new FilterableTreeItem<>(rootComponent);
root.getInternalChildren().setAll(childNode1, childNode2, childNode3);
root.setPredicate((parent, value) -> value.isSelected());

// TreeTableView
final TreeTableView<Component> treeTableView = new TreeTableView<>(root);

Note that you have to use getInternalChildren() to add children and the default getChildren().

FilterableTreeItem<T> also provides a predicateProperty() that you can bind to another property in case you need to update the how items are shown or hidden.

Another advatage of this class is that it shows the whole path up to the root of the items matching that predicate.

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36