B4J Question [SOLVED] Get last object focused

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
How to use getMostRecentFocusOwner() for get the latest node who received the focus?
Thanks
 

stevel05

Expert
Licensed User
Longtime User
getMostRecentFocusOwner() is an AWT method on a Window object, assuming you are not using AWT but are using JavaFX (Default in B4j) then you can use getFocusOwner on the Scene:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
  
    Sleep(0)
    Dim FormJO As JavaObject = MainForm
    Dim Scene As JavaObject = FormJO.GetField("scene")
    Log(Scene.RunMethod("getFocusOwner",Null))
End Sub

Will return null if there is no focused view. You may not need the Sleep(0) depending on where you call it from.
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Thanks, stevel05

Now if i have this output
B4X:
TextField@76c85[styleClass=text-input text-field]
how i can get the reference of this node? (tag, text ...)
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
B4X:
    Dim FormJO As JavaObject = MainForm
    Dim Scene As JavaObject = FormJO.GetField("scene")
    dim selectedNode as node
    Try
         selectedNode = Scene.RunMethod("getFocusOwner",Null)
         Log(selectedNode.Tag) ' all node methods available
     Catch
         Log("no node has focus")
      End Try
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
To get the text:

B4X:
Dim SelectedNode As Node = Scene.RunMethod("getFocusOwner",Null)
   
    If SelectedNode Is TextField Then
        Dim TF As TextField = SelectedNode
        Log(TF.Text)
    End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…