在自定义TreeItem属性更改上更新TreeView [英] Update TreeView on custom TreeItem property change

查看:145
本文介绍了在自定义TreeItem属性更改上更新TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扩展了TreeCell和TreeItem类。 MyTreeItem包含一个自定义属性,我在MyTreeCell中使用它来渲染图形/字体等。问题是当我设置MyTreeCell.customProperty我不知道如何使TreeView / Cell重绘。

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.

例如:

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.

推荐答案

至少有两种方法(不包括将值归零的黑客,如评论中所建议的那样)

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

一种是在设置自定义属性时手动触发TreeModificationEvent,即在setCustomProperty中:

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);
    }
}

另一种方法是将自定义属性设为真实属性,让感兴趣的各方(你的自定义TreeCell)听取该属性的变化。有关如何实现(和重新连接)侦听器的示例,请查看DefaultTreeCell如何处理TreeItem的graphicProperty。

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.

选择哪个取决于您的上下文:第一个确保通知TreeModificationEvents的所有侦听器,第二个允许实现一般TreeCell,使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.

这篇关于在自定义TreeItem属性更改上更新TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆