B4J Question How do you get TreeItem from mouse click?

TorontoJim

Member
Licensed User
Longtime User
With a TreeView MouseClick event, I can get the EventData for the MouseClick, and I can get the Sender, which is the TreeView.Root.

Is there a way to get the actual TreeItem node? Or do I have store the SelectedItemChanged in a global var and then access it from the MouseClick event sub?

I want to be able to have a double click on a TreeItem send that Item to another subroutine. I'm cool with the global var solution, I just want to know if there is a more elegant way.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
Sub TreeView1_MouseClicked (EventData As MouseEvent)
   Dim jo As JavaObject = EventData
   Dim target As JavaObject = jo.RunMethodJO("getTarget", Null)
   Do Until target Is TreeView
     If GetType(target) = "com.sun.javafx.scene.control.skin.TreeViewSkin$1" Then
       Dim ti As TreeItem = target.RunMethod("getTreeItem", Null)
       Log($"Clicked tree item: ${ti}"$)
       If EventData.ClickCount = 2 Then
         Log("double click!")
       End If
       Exit
     End If
     Dim n As Node = target
     target = n.Parent
   Loop
End Sub
 
Upvote 0
Top