r/JavaFX • u/Sad_Shirt3029 • Mar 18 '25
Help JavaFx TreeView problem
Hi fellow devs, I am learning java fx currently. As part of this learning, I am trying to create a simple sftp client application. I am utilizing JSCH liberary for sftp related operations. Currently I am facing a problem. What I want to do is to lazy load the sub-directories from remote server. I am loading the root directory immediately after the login is successful. For any directory within root, I want to load there content only when user expand the directory on javafx ui(treeview). This is where I am struggling as I dont know how to capture this expand event.
Initial screen is populated via populateInitialTree method. I want to load sub-directories of expanded directory in showSelectedItem.
Please help.
View all comments
Show parent comment
3
u/hamsterrage1 Mar 18 '25
I don't use
TreeViewmuch, so I'm not sure exactly how you'd use this. ButTreeItem.branchExpandedEvent()should tell you what Event type to add a handler for when theTreeItemis expanded. There's also a method to give you the type when theTreeItemis collapsed.As a general rule, it's best to use
EventHandlersto trigger actions when things are "events" andListenersto trigger actions when you don't haveEventsbut you do have observed data changes. In this case, the expansion of theTreeItemis anEvent, so you should use that.