Update TreeView on custom TreeItem property change

Deadly 提交于 2020-01-15 15:57:27

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/30556259/update-treeview-on-custom-treeitem-property-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!