# Treeview component: Detect when expand button is pressed

**URL:** <https://forum.inductiveautomation.com/t/treeview-component-detect-when-expand-button-is-pressed/82504>\
**Category:** Ignition\
**Tags:** vision, scripting\
**Created:** [November 30, 2023, 12:59am UTC](https://forum.inductiveautomation.com/t/treeview-component-detect-when-expand-button-is-pressed/82504 "2023-11-30T00:59:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hyphriend](https://avatars.discourse-cdn.com/v4/letter/h/f04885/32.png) [@Hyphriend](https://forum.inductiveautomation.com/u/Hyphriend)\
**Post date:** [November 30, 2023, 12:59am UTC](https://forum.inductiveautomation.com/t/treeview-component-detect-when-expand-button-is-pressed/82504/1 "2023-11-30T00:59:19Z")

</div>

Hi all!

I want to display a lot of categorized data with a treeview component.

Instead of loading the data all at once, i want to run a script that updates the dataset when one of the expand button is pressed.

Is there a way to detect when the expand button is pressed?

Pressing this:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/2/32a98c2135e8f485074544390863e921a995ef60.png)  
Instead of this:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/0/00242606d19e80a1d82473de01567f766e232343.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 30, 2023, 4:26pm UTC](https://forum.inductiveautomation.com/t/treeview-component-detect-when-expand-button-is-pressed/82504/2 "2023-11-30T16:26:08Z")

</div>

Yes, this is technically possible, by accessing the inner Swing JTree and adding a listener to it:

> **[JTree (Java SE 17 & JDK 17)](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/JTree.html#addTreeWillExpandListener(javax.swing.event.TreeWillExpandListener))**
>
> declaration: module: java.desktop, package: javax.swing, class: JTree

However, I'm pretty sure just overwriting the dataset won't work - it will immediately collapse all the nodes again.

We don't have any readily available examples of generating a properly lazy tree.  
If you don't mind some Swing hacking, you could create your own tree model and populate it with instances of our `com.inductiveautomation.ignition.client.util.gui.tree.AbstractLazyLoadingTreeNode` class entirely using scripting. That class has an abstract [`loadChildren()`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.34/com/inductiveautomation/ignition/client/util/gui/tree/AbstractLazyLoadingTreeNode.html#loadChildren()) method on it that will be fired to actually initialize each node as it's expanded.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [September 18, 2024, 6:32pm UTC](https://forum.inductiveautomation.com/t/treeview-component-detect-when-expand-button-is-pressed/82504/4 "2024-09-18T18:32:06Z")

</div>

I suggest that you start a new thread and tag it as perspective. Don't mix this one up.
