Private Sub btn_Click
Dim btn As B4XView = Sender
Log(btn.Text)
End Sub
that is not the view he is trying to get the click event to trigger!As you are creating the button in the Main module, it will automatically get the default events which will be called in the module in which the views are created, so you just need to add the sub in the Main module:
Public Sub AddFlexChild(v As B4XView, width As Int, height As Int)
nView = v
mBase.AddView(nView, 0, 0, width, height)
Children.Add(nView)
' Attach click event if callback exists
If xui.SubExists(mCallBack, mEventName & "_Click", 1) Then
nView.Tag = "justflexbox_child"
#If B4J
Dim jo As JavaObject = nView
jo.RunMethod("setOnMouseClicked", Array(jo.CreateEvent("javafx.event.EventHandler", "FlexChild_Clicked", True)))
#End If
End If
LayoutChildren
End Sub
You are Right, as usual....B4X:Public Sub AddFlexChild(v As B4XView, width As Int, height As Int) nView = v mBase.AddView(nView, 0, 0, width, height) Children.Add(nView) ' Attach click event if callback exists If xui.SubExists(mCallBack, mEventName & "_Click", 1) Then nView.Tag = "justflexbox_child" #If B4J Dim jo As JavaObject = nView jo.RunMethod("setOnMouseClicked", Array(jo.CreateEvent("javafx.event.EventHandler", "FlexChild_Clicked", True))) #End If End If LayoutChildren End Sub
v As B4xview is the button created in Main and passed to the JustFlexbox Class, the event is already attached at this point, the method only creates an array of the views. Check my example project.
Not so, the buttons are created in Main asSender is only useful if the views are created using the Visual Designer, in which the views event name can be set!
Dim btn As Button
btn.Initialize("btn")
it is ilogic for me to the buttons be created outside the class
B4X:Public Sub AddFlexChild(v As B4XView, width As Int, height As Int) nView = v mBase.AddView(nView, 0, 0, width, height) Children.Add(nView) ' Attach click event if callback exists If xui.SubExists(mCallBack, mEventName & "_Click", 1) Then nView.Tag = "justflexbox_child" #If B4J Dim jo As JavaObject = nView jo.RunMethod("setOnMouseClicked", Array(jo.CreateEvent("javafx.event.EventHandler", "FlexChild_Clicked", True))) #End If End If LayoutChildren End Sub
v As B4xview is the button created in Main and passed to the JustFlexbox Class, the event is already attached at this point, the method only creates an array of the views. Check my example project.
What @Chris Guanzon is trying to do is precisely a custom view, a container to which views can be added at runtime.Custom views are different, the views used need to be created within the custom view itself and events on the contained views need to be handled in the Custom View class and passed to the parent module as required. Otherwise it wouldn't be a custom view but a container.
True, but it's a Custom View for the flexbox, and the buttons passed are it's children. As would be with a Pane or similar. An edge case.What @Chris Guanzon is trying to do is precisely a custom view.
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
Private Pane1 As Pane
Private fbv As JustFlexbox
Private index As Int = 0
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
' Initialize JustFlexbox with Pane1 as base
fbv.Initialize(Pane1, "fbv")
fbv.FlexDirection = "row"
fbv.Wrap = True
fbv.GapX = 10dip
fbv.GapY = 10dip
fbv.JustifyContent = "center"
fbv.AlignItems = "center"
' Add buttons dynamically
For i = 1 To 12
Dim btn As Button
btn.Initialize("btn")
btn.Text = "Btn " & i
btn.Tag = index
Dim w As Int = 80 + Rnd(0, 40)
Dim h As Int = 50
fbv.AddFlexChild(btn, w, h)
index = index + 1
Next
For i = 1 To 8
Dim btn As Button
btn.Initialize("btn")
btn.Text = "Btn " & i
btn.Tag = index
fbv.AddFlexChild(btn, 80dip, 50dip)
index = index + 1
Next
End Sub
Private Sub fbv_Click(view As B4XView)
Log("Clicked: " & view.Text)
End Sub
Private Sub btn_MouseClicked(EventData As MouseEvent)
Dim btn As Button = Sender
Log("mouse clicked " & btn.Text & " tag = " & btn.tag)
Log("view text " & fbv.GetBase.GetView(btn.Tag).Text)
End Sub
Log("view width " & fbv.GetBase.GetView(btn.Tag).Width)