shnplr
shnplr

Reputation: 230

Update TreeView on custom TreeItem property change

I have extended TreeCell and TreeItem class. MyTreeItem contains a custom property which I use inside MyTreeCell to render graphics/font etc. The problem is when I set MyTreeCell.customProperty I'm not sure how to make the TreeView/Cell redraw.

For example:

public class MyTreeItem extends TreeItem {
    Object customProperty

    public void setCustomProperty(Object customProperty) {
        this.customProperty = customProperty

        // how to fire a change event on the TreeView?
    }
}

Any comments on the solution or (lack of) design approach appreciated.

Upvotes: 2

Views: 2299

Answers (1)

kleopatra
kleopatra

Reputation: 51525

There are at least two approaches (not including the hack of nulling the value, as suggested in the comments)

One is to manually fire a TreeModificationEvent when setting the custom property, that is in your setCustomProperty:

public class MyTreeItem extends TreeItem {
    Object customProperty

    public void setCustomProperty(Object customProperty) {
        this.customProperty = customProperty
        TreeModificationEvent<T> ev = new TreeModificationEvent<>(valueChangedEvent(), this);
        Event.fireEvent(this, ev);
    }
}

Another is to make the custom property a "real" property and let interested parties (f.i. your custom TreeCell) listen to changes of that property. For an example of how to implement (and re-wire) the listener have a look at how DefaultTreeCell handles the graphicProperty of a TreeItem.

Which to choose depends on your context: the first makes sure that all listeners to TreeModificationEvents are notified, the second allows to implement a general TreeCell taking a property (factory) of the treeItem to visualize.

Upvotes: 3

Related Questions