B4J Tutorial TreeView Tutorial

Status
Not open for further replies.
B4J v1.05 adds support for the TreeView node.

TreeView as it name suggests visualizes a tree structure.

Each item in the TreeView is a TreeItem (or CheckBoxTreeItem). Each item has a list of children items.

Example:
B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private TreeView1 As TreeView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
   TreeView1.Initialize("TreeView1") 'here we create it by code. Usually you will add it with the builder.
   MainForm.RootPane.AddNode(TreeView1, 0, 0, 400, 400)
   'add 10 items, each with a child item
   For i = 1 To 10
     Dim ti As TreeItem
     ti.Initialize("ti", "Item #" & i)
     Dim cti As TreeItem
     cti.Initialize("ti", "My father is Item #" & i)
     ti.Children.Add(cti) 'add the child
     TreeView1.Root.Children.Add(ti) 'add the father to the root
   Next
End Sub

SS-2013-12-11_16.10.27.png


Items are added to the root TreeItem. The root itself is not visible.

You can add an image to each of the items by setting the Image property.

TreeItem raises a single event which is the ExpandedChanged event. You can use the Sender keyword to get the TreeItem that raised the event:
B4X:
Sub TI_ExpandedChanged(Expanded As Boolean)
   Dim ti As TreeItem = Sender
   Log(ti.Text & ": " & Expanded)
End Sub

CheckBoxTreeItem

The TreeView can show checkboxes before each of the items.
This is done by:
- Calling TreeView.TreeView1.SetCheckBoxesMode
- Adding CheckBoxTreeItems instead of TreeItems.

SS-2013-12-11_16.18.13.png


CheckBoxTreeItems raise the CheckedChange event (and ExpandedChanged event).
 

devlei

Active Member
Licensed User
Longtime User
I have looked at the TreeViewClass_8 for B4A. Does it have the same functionality as this B4J TreeView or is there another B4A equivalent?
 

ribber

Member
Licensed User
Longtime User
Is there a possibility to get the current selected item? E.g. an event called TreeItemClicked(Text As String).

Thank you.
 

stevel05

Expert
Licensed User
Longtime User
It's actually,

B4X:
SelectedItemChanged(SelectedItem As TreeItem)

You can then do:
B4X:
Sub TreeView1_SelectedItemChanged(SelectedItem As TreeItem)
    Text = SelectedItem.Text
End Sub
 

derez

Expert
Licensed User
Longtime User
devlei wrote:
I have looked at the TreeViewClass_8 for B4A. Does it have the same functionality as this B4J TreeView or is there another B4A equivalent?
I have just made b4j version of b4a Treeview class but Erel works faster. I haven't been able to check it yet and can't compare the functionality.
 

devlei

Active Member
Licensed User
Longtime User
A B4A Treeview View would really be great!!! For the non-professionals like myself it just seems easier to incorporate a view than class.
 

derez

Expert
Licensed User
Longtime User
Like Erel said, in android there isn't such view, so if you do want to use a treeview you can either use a class that has been made to cover this absence, or create such by yourself to suit your own needs.
In the b4j 1.05 there is a view and you don't need a special class, unless you need additional things to what the view provides.
 

derez

Expert
Licensed User
Longtime User
1. How do I remove treeitems from the treeview when I don't know their name ?
The treeview doesn't have getallnodes or something similar (clear ? , removeallnodes?)
2. Is it possible to have a tag or some other property to store some data about the treeitem ?
3. Any way to set the height of the treeitem ?
4. Any way to style the text and the background ?

Edit: found answer to 4 - by treeview.style but same style goes to all treeitems.
No 3 - the height is set by the image size, so I use image initializesample with the required height. For items without picture - I put a blank picture with the same size.
Answer to 1 - until better is given, I remove the treeview from its parent and put one again by code.
 
Last edited:

Rzimski

Member
Hi everyone

I tried to get the parent from a treeItem but i always get an error
B4X:
Private treeItem2 As TreeItem
    treeItem2 = SelectedItem.Parent
    Log(treeItem2.Text)

Thank you very much
 

LucaMs

Expert
Licensed User
Longtime User
I'm wrong, Erel, or you can change the TreeView?
It would be very useful to have a property that indicates the level of depth of the item selected.

(move in "I Wish"?)

P.S. May be i can calculate that level, using recursion on parent (grrr my english, sorry)

"It would be very useful to have" static local variables
 

LucaMs

Expert
Licensed User
Longtime User
I would like to try to create a query builder, based on Erel's SQL Viewer.

I have already made some changes, but now I'm stuck.

I would like to drag a TreeItem in the builder but it has only one event, ExpandedChanged.

I have no way of knowing the current item.

Can I use these coordinates, in some way, to get to the current item?


B4X:
Sub tvwTables_MousePressed (EventData As MouseEvent)
      EventData.X
end Sub
 

Cableguy

Expert
Licensed User
Longtime User
Hi guys,

Is it possible to have an "editable" item in treeview?
 
Status
Not open for further replies.
Top