B4J Question Button Array hover get tag

alienhunter

Active Member
Licensed User
Longtime User
Hi ,
I have 270 buttons array created at runtime , but now how can I extract the tag of single an button when the mouse hovers over an certain button without clicking it ?
I have found examples but not in an array of buttons
Thank you AH


 

Daestrum

Expert
Licensed User
Longtime User
Example that you can modify - needs JavaObject Library
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim b(10) As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    For a = 0 To 9
        b(a).Initialize("")
        b(a).Id = ""&a
        b(a).SetSize(10,10)
        MainForm.RootPane.AddNode(b(a),a*12,0,-1,-1)
        MouseOver(b(a))
        Next  
End Sub
Sub MouseOver(n As Object)
    Dim n1 As Button = n
        setHandler(n1,"setOnMouseEntered","mouseIn")
        setHandler(n1,"setOnMouseExited","mouseOut")
End Sub
Sub setHandler(ob As JavaObject,eventName As String,handlerName As String)
ob.RunMethod(eventName, Array(ob.CreateEventFromUI("javafx.event.EventHandler",handlerName,True)))
End Sub
Sub mouseIn_Event(m As String,args() As Object)
    Log("In "& asJO(Sender).RunMethod("getId",Null))
End Sub
Sub mouseOut_Event(m As String,args() As Object)
    Log("Out "& asJO(Sender).RunMethod("getId",Null))
End Sub
Sub asJO(o As JavaObject) As JavaObject
    Return o
End Sub

If you are using Tag in place of Id then change the calls in mouseIn_Event & mouseOut_Event to
B4X:
Log(asJO(sender).RunMethod("getUserData",Null))
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Another implementation:
B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Type ButtonData (x As Int, y As Int)
   Private lastButton As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.SetFormStyle("UNIFIED")
   MainForm.Show
   For x = 0 To 19
     For y = 0 To 19
       Dim b As Button
       b.Initialize("button")
       MainForm.RootPane.AddNode(b, 50 * x, 40 * y, 45, 35)
       b.Text = x & "," & y
       Dim bd As ButtonData
       bd.x = x
       bd.y = y
       b.Tag = bd
     Next
   Next
End Sub

Sub button_MouseMoved (EventData As MouseEvent)
   If lastButton = Sender Then Return
   lastButton = Sender
   Dim bd As ButtonData = lastButton.Tag
   MainForm.Title = $"Hover over button: ${bd.x} / ${bd.y}"$
End Sub

Sub MainForm_MouseMoved (EventData As MouseEvent)
   lastButton = Null
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…