B4J Question Treeview checkedboxitem

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
I have same problems with checkedboxitem.

In Process_Globals:
Dim itemtreebox As CheckboxTreeItem and treearc is a TreeView
B4X:
Sub caricatree
    Dim l As List
    Dim res() As String
    Dim img As Image
    img.Initialize(File.DirAssets, "table.png")
    l.Initialize
    l = DBUtils.ExecuteMemoryTable(SQL1, "SELECT name, sql FROM sqlite_master WHERE type='table' ", Null, 0)
    Dim i As Int
    Dim itemup As TreeItem
    itemup.Initialize("itemup", "Archivi")
    itemup.Image = img
    For i = 2 To l.Size -1
        res = l.Get(i)
        itemtreebox.Initialize("itemtreebox", res(0))
        itemup.Children.Add(itemtreebox)
    Next
     treearc.Root.Children.Add(itemup)
    treearc.SetCheckBoxesMode
    treearc.Root.Expanded = True   
End Sub
In treearc appears the entire correct list with ceckbox but i can not understand
as control which ceckboxitem are checked and how to activate event on ceckboxitem click.
I tried with:
Sub treearc_CheckedChange or itemtrrebox_CheckedChange but not work.
Only Sub treearc_SelectedItemChanged(SelectedItem As TreeItem) is raised.
Thanks
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

use like below - There might be other solutions.
B4X:
Sub CheckboxItem_CheckedChange(Checked As Boolean)
    Dim CheckboxItem As CheckboxTreeItem = Sender
    Log("CheckBoxItem state changed to " & Checked & " for " & CheckboxItem.Text)
End Sub

B4X:
Sub TreeView1_SelectedItemChanged(SelectedItem As TreeItem)
   Dim CheckboxItem As CheckboxTreeItem = SelectedItem
   Log("SelectedItemChanged: " & SelectedItem.Text & ", State is " & CheckboxItem.Checked)
End Sub

The events are described in the documentation:
https://www.b4x.com/b4j/help/jfx.html#checkboxtreeitem

Pls find simple testproject attached.
 

Attachments

  • TreeViewCheckItems.zip
    4.1 KB · Views: 238
Last edited:
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Ok, thanks rwblinn, now work.
Is possible ceck in a treeviwe wich item are checked with a iteration
B4X:
For each cbt as CheckboxTreeItem in TreeView.....
    Dim check as boolean = cbt.Checked
Next
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

use like
B4X:
For i = 0 To TreeView1.Root.Children.Size - 1
  Dim CheckboxItem As CheckboxTreeItem = TreeView1.Root.Children.Get(i)
  Log(CheckboxItem.Text & " = " & CheckboxItem.Checked)
Next

Pls note correction in Post #2 for TreeView1_SelectedItemChanged.
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
For i = 0 To TreeView1.Root.Children.Size - 1
returns only the value 1 (the first item), the subitem rightly are not counted and if i add:
B4X:
Dim treeitem As TreeItem = treearc.Root.Parent
treeitem is always empty (not initialized).
Good day to all.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Test if this works to get all items in the treeview populated with checkboxitems.

B4X:
Private Sub CollectTreeItems(TreeItems As List, Items As List)
    For Each ti As TreeItem In Items
        Dim CheckboxItem As CheckboxTreeItem = ti
        TreeItems.Add(ti.Text & " = " & CheckboxItem.Checked)
        If ti.Children.Size > 0 Then
            CollectTreeItems(TreeItems, ti.Children)
        End If
    Next
End Sub

Then call like
B4X:
Dim TreeList As List
TreeList.Initialize
CollectTreeItems(TreeList, TreeView1.Root.Children)
For i = 0 To TreeList.Size - 1
    Log(TreeList.Get(i))
Next
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
This line code in Sub CollectTreeItems
B4X:
TreeItems.Add(ti.Text & " = " & CheckboxItem.Checked)
generates the following exception
B4X:
Program started.
Error occurred on line: 742 (main).
java.lang.RuntimeException: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at anywheresoftware.b4j.objects.TreeViewWrapper$CheckBoxTreeItemWrapper.getChecked(TreeViewWrapper.java:213)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:563)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:221)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:156)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:82)
    at anywheresoftware.b4a.BA$2.run(BA.java:165)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1593970726.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/254413710.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/326549596.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4j.objects.TreeViewWrapper$CheckBoxTreeItemWrapper.getChecked(TreeViewWrapper.java:210)
    ... 24 more

but the problem is that
treearc.Root.Children return 1 as size (the first item and not the subitem).
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Ok, work.
The problem is that the first item is a TreeItem and the subItem are CheckboxTreeItem, than
B4X:
TreeItems.Add(ti.Text & " = " & CheckboxItem.Checked)
generates an exception.
Thanks
 
Upvote 0
Top