B4J Question Menu Click Callback

alizeti

Member
Licensed User
Hi,

I was wondering if possible to generate a callback using menu bar that has no submenu.

Basically, my menu bar will have 2 options : "open" and "about"

There is no submenu for these 2 options. So if the user clicks "open", a callback will be trigger and samething if he clicks "about"

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The top level menus do not raise the click event.

You can put a label inside the menu item. It will not behave exactly like a regular menu item.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
    ReplaceMenuWithLabel("File", "FileMenu")
End Sub

Sub ReplaceMenuWithLabel(Text As String, EventName As String)
    Dim lbl As Label
    lbl.Initialize(EventName)
    lbl.Text = Text
    For Each m As Menu In MenuBar1.Menus
        If m.Text = Text Then
            m.Text = ""
            Dim jo As JavaObject = m
            jo.RunMethod("setGraphic", Array(lbl))
        End If
    Next
End Sub

Sub FileMenu_MouseClicked (EventData As MouseEvent)
    Log("click")
End Sub

Make sure to set the correct text in the designer (File and not _File for example).
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Here is another workaround to try, it relies on the fact that the menu can be hidden before is is drawn, and requires a dummy MenuItem to be set on the menu, otherwise the OnShowing event is not raised. It hijacks the Menus onShowing event.

Depends on JavaObject

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private MenuBar1 As MenuBar
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("menu") 'Only contains a MenuBar
  
    CreateMenus
  
    MainForm.Show
  
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Public Sub CreateMenus
    MenuBar1.Menus.Clear
  
    Dim About As Menu
    About.Initialize("About","")
  
    Dim MI As MenuItem
    MI.Initialize("Dummy","")
    About.MenuItems.Add(MI)
  
    Dim SomethingElse As Menu
    SomethingElse.Initialize("Something Else","")
  
    Dim MI As MenuItem
    MI.Initialize("Dummy","")
    SomethingElse.MenuItems.Add(MI)
  
  
    MenuBar1.Menus.Add(About)
    MenuBar1.Menus.Add(SomethingElse)
  
  
    SetOnShowing(About)
    SetOnShowing(SomethingElse)
  
End Sub

Private Sub SetOnShowing(Menu1 As JavaObject)
    Dim Event As Object = Menu1.CreateEventFromUI("javafx.event.EventHandler","MenuShowing",Null)
    Menu1.RunMethod("setOnShowing",Array(Event))
End Sub

Private Sub MenuShowing_Event (MethodName As String, Args() As Object)
  
    Dim M As Menu = Sender
    Dim MJO As JavaObject = M

    Select M.Text
        Case "About"
            MJO.RunMethod("hide",Null)
            Log(M.Text & " Clicked")
        Case "Something Else"
            MJO.RunMethod("hide",Null)
            Log(M.Text & " Clicked")
    End Select
  
End Sub

Edit: This won't work with shortcuts either.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…