Wish TreeViewItem Key Property Addition

Mashiane

Expert
Licensed User
Longtime User
Hi

At the current state, the TreeViewItem is indexed by a Text property to do searches, update it etc. This provides a challenge when one needs to have the same text property in the tree at different locations or parent items.

Is it possible to have a "key" property for a TreeViewItem that will be used to add an item to the tree and not the Text Property used?

As an example, I can't do this at the moment in a unique way.

Book 1
Chapter 1
Chapter 2
Book 2
Chapter 1
Chapter 2
Chapter 3

To remove Chapter 3 of Book 2, first I need to search for Book 2 then search for Chapter 3, however with keys, I could just pass a key variable e.g. Book2.Chapter3 as a key to Chapter 3 of Book 2.

Also storing these in a map using the Text property, the map will be overwritten due to duplicates in the TreeView Text properties.

Any considerations?

Thanks.
 

jmon

Well-Known Member
Licensed User
Longtime User
you could store the index of the nodes in a map, i.e:

B4X:
Dim m As Map
m.Initialize

For parent = 1 To 10
    Dim ti As TreeItem
    ti.Initialize("", parent)
 
    For child = 1 To 5
        Dim tiChild As TreeItem
        tiChild.Initialize("", child)
        ti.Children.Add(tiChild)
     
        'store the parentIndex / ChildIndex
        m.Put(parent & "/" & child, tiChild)
    Next
    
    tv1.Root.Children.Add(ti) 
 
    'store the parentIndex
    m.Put(parent, ti)
Next

'rename child 2/3:
Dim ti As TreeItem = m.Get("2/3")
ti.Text = "Renamed"
ti.Parent.Expanded = True
Here I store the treeItems by index, but you could store them another way.

This is the output
Capture.PNG
 

jmon

Well-Known Member
Licensed User
Longtime User
I checked, and there is no possibility to set a "Tag" (or "key") to the treeItem:
usually this should have worked:
B4X:
Dim tiJo As JavaObject = tiChild
tiJo.RunMethod("setUserData", Array(Rnd(0, 99999)))
but this doesn't exist for the treeItem in javafx:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TreeItem.html#valueProperty
This is because treeItem is not a node.

So because it's missing in Javafx, you will have to do it with map.
 
Top