B4J Question Treeview checkedboxitem

Discussion in 'B4J Questions' started by micro, Sep 19, 2015.

  1. micro

    micro Well-Known Member Licensed User

    Hi to all
    I have same problems with checkedboxitem.

    In Process_Globals:
    Dim itemtreebox As CheckboxTreeItem and treearc is a TreeView
    Code:
    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' "Null0)
        
    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
     
  2. rwblinn

    rwblinn Well-Known Member Licensed User

    Hi,

    use like below - There might be other solutions.
    Code:
    Sub CheckboxItem_CheckedChange(Checked As Boolean)
        
    Dim CheckboxItem As CheckboxTreeItem = Sender
        
    Log("CheckBoxItem state changed to " & Checked & " for " & CheckboxItem.Text)
    End Sub
    Code:
    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:
    http://www.b4x.com/b4j/help/jfx.html#checkboxtreeitem

    Pls find simple testproject attached.
     

    Attached Files:

    Last edited: Sep 20, 2015
  3. micro

    micro Well-Known Member Licensed User

    Ok, thanks rwblinn, now work.
    Is possible ceck in a treeviwe wich item are checked with a iteration
    Code:
    For each cbt as CheckboxTreeItem in TreeView.....
        
    Dim check as boolean = cbt.Checked
    Next
     
  4. rwblinn

    rwblinn Well-Known Member Licensed User

    Hi,

    use like
    Code:
    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.
     
  5. micro

    micro Well-Known Member Licensed User

    Thank you very much.
     
  6. micro

    micro Well-Known Member Licensed User

    returns only the value 1 (the first item), the subitem rightly are not counted and if i add:
    Code:
    Dim treeitem As TreeItem = treearc.Root.Parent
    treeitem is always empty (not initialized).
    Good day to all.
     
  7. rwblinn

    rwblinn Well-Known Member Licensed User

    Test if this works to get all items in the treeview populated with checkboxitems.

    Code:
    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
    Code:
    Dim TreeList As List
    TreeList.Initialize
    CollectTreeItems(TreeList, TreeView1.Root.Children)
    For i = 0 To TreeList.Size - 1
        
    Log(TreeList.Get(i))
    Next
     
  8. micro

    micro Well-Known Member Licensed User

    This line code in Sub CollectTreeItems
    Code:
    TreeItems.Add(ti.Text & " = " & CheckboxItem.Checked)
    generates the following exception
    Code:
    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).
     
  9. micro

    micro Well-Known Member Licensed User

    Ok, work.
    The problem is that the first item is a TreeItem and the subItem are CheckboxTreeItem, than
    Code:
    TreeItems.Add(ti.Text & " = " & CheckboxItem.Checked)
    generates an exception.
    Thanks
     
    rwblinn likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice