2

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.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
shnplr
  • 230
  • 3
  • 12
  • Can't you just use a default `TreeItem` implementation and use its `value` property for this use case? The tree cell will by default observe that property, so you wouldn't need any additional wiring like this. – James_D May 31 '15 at 12:35
  • I have special items which have a display value but are not associated with my model. I may be able to create a class instead which wraps the display value and associated object. – shnplr May 31 '15 at 14:59
  • Try to clear and re-set the valueProperty, it may trigger the updateItem which then updates your cell rendering. – Uluk Biy Jun 01 '15 at 05:01

1 Answers1

3

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.

kleopatra
  • 51,061
  • 28
  • 99
  • 211