B4J Question Number of nodes on form pane doubles

Discussion in 'B4J Questions' started by emexes, Jul 18, 2019.

  1. emexes

    emexes Well-Known Member Licensed User

    I load a "flat" single-variant layout with five nodes on it. Nothing fancy: just 4 buttons and a text field.

    I count the number of nodes using GetAllViewsRecursive:

    # before loading the layout: 0 nodes - no worries
    # after loading the layout: 5 nodes - no worries
    # after MainForm.Show: 10 nodes - huh?
    # after another MainForm.Show: 10 nodes - well, at least it's stable

    What are the extra nodes for? I could imagine they are created during window (re)sizing, but... why are they not deleted? Should they be? Can I ignore them? Are they always -1 width and height?
    Code:
    Sub AppStart (Form1 As Form, Args() As String)
     
        MainForm = Form1
     
                
    Dim NumNodes0 As Int  = 0
                
    For Each N As Node In MainForm.RootPane.GetAllViewsRecursive
                    NumNodes0 = NumNodes0 + 
    1
                
    Next
     
        MainForm.RootPane.LoadLayout(
    "Layout1"'Load the layout file.
     
                
    Dim NumNodes1 As Int  = 0
                
    For Each N As Node In MainForm.RootPane.GetAllViewsRecursive
                    NumNodes1 = NumNodes1 + 
    1
                
    Next
     
        MainForm.Show
     
                
    Dim NumNodes2 As Int  = 0
                
    For Each N As Node In MainForm.RootPane.GetAllViewsRecursive
                    NumNodes2 = NumNodes2 + 
    1
                
    Next
     
        MainForm.Show
     
                
    Dim NumNodes3 As Int  = 0
                
    For Each N As Node In MainForm.RootPane.GetAllViewsRecursive
                    NumNodes3 = NumNodes3 + 
    1
                
    Next
     
        
    Log("NumNodes = " & NumNodes0 & " " & NumNodes1 & " " & NumNodes2 & " " & NumNodes3)
     
        
    For Each N As Node In MainForm.RootPane.GetAllViewsRecursive
            
    Log(N.Left & " " & N.Top & " " & N.PrefWidth & " " & N.PrefHeight)
        
    Next
     
    End Sub
    produces log:
    Code:
    Waiting for debugger to connect...
    Program started.
    NumNodes = 
    0 5 10 10
    10 10 100 30
    37 5 -1 -1
    370 10 100 30
    37 5 -1 -1
    10 320 100 30
    37 5 -1 -1
    380 330 100 30
    37 5 -1 -1
    20 50 440 260
    206 120 -1 -1
     
  2. Daestrum

    Daestrum Well-Known Member Licensed User

    GetAllViewsRecursive is returning the correct count. A button is 2 nodes (views) a Button and a LabelledText node (the text of the button)

    To get the nodes only (not sub nodes) use
    Code:
    For a = 0 To MainForm.RootPane.NumberOfNodes -1
      
    Dim n As Node = MainForm.RootPane.GetNode(a)
      
    Log(N.Left & " " & N.Top & " " & N.PrefWidth & " " & N.PrefHeight)
     
    Next
    Or if you want to get child count often use a sub like (uses javaobject)
    Code:
    Sub childCount(j As JavaObjectAs Int
     
    Return j.RunMethodjo("getChildren",Null).RunMethod("size",Null)
    End Sub
    and call with
    Code:
    ...
    Log(childCount(Mainform.RootPane))
    ...
     
    Last edited: Jul 18, 2019
    Erel likes this.
  3. emexes

    emexes Well-Known Member Licensed User

    The update to your post answered my half-written follow-up query :)

    Thank you.
     
  4. Daestrum

    Daestrum Well-Known Member Licensed User

    Or you could use
    Code:
    Log(Mainform.RootPane.NumberOfNodes)
     
    Erel 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