How to access custom Component in TreeViewItem?


So I’ve got a TreeView where each TreeViewItem is a custom Component (using createItemComponent). But the docs say I shouldn’t keep a reference to the component. So how am I supposed to “get at it” if I need to update something in it?


why do you need to ‘get at it’?

Typically the component will be self maintaining. You can give it a pointer to the owner TreeView. Equally you can give it a pointer or a handle to some data object that the component is representing. You wouldn’t normally need to go in the other direction though.


Basically the app needs to send a message to the component to update itself because the underlying data has changed. Is there a better way to do that? I thought about using a ChangeListener but it seemed like I would run into the same problem of not being able to keep a pointer to the component.


Sounds like you’re not quite grasping the way that changelisteners are supposed to be used. Don’t think of it as “the app sending a message to the component”, but rather as “the component registering with the app for change callbacks”.

Just make the app a changebroadcaster, then in your component’s constructor, make it register itself with the app for change callbacks. Then in its destructor, make it unregister itself. No need for the app to know anything about the components directly.


Ahh perfect. Thanks.