B4J Question Menubar - Make menu title clickable?

tchart

Well-Known Member
Licensed User
Longtime User
Hi, I have a menubar with a few child menus. I need to implement clickable item on the toolbar - this is so the user doesnt have to click the menu to activate a tool.

I can change the text out for an image which is fine but there doesnt seem to be an event for clicking the menu parent item. I have found some code that will add an event using the "OnShowing" event but since this item has no children the event never fires - there is no OnMouseClick on the menu as far as I can see in the JavaDocs.

Closest thing Ive found is to use a label; https://stackoverflow.com/questions/48017645/event-handler-in-javafx-for-menu

Does anyone have any solutions before I try the above?

1603072175538.png

1603072288680.png
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
may be you already have figured the label part, i think that one is a nice solution and very easy to implement

B4X:
    Dim menu As MenuBar
    Dim jo As JavaObject = menu.Menus.Get(0)
    
    Dim lbl As Label
    lbl.Initialize("yourLabel")
    lbl.Text = "lol"
    jo.RunMethod("setGraphic",Array(lbl))
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
may be you already have figured the label part, i think that one is a nice solution and very easy to implement

B4X:
    Dim menu As MenuBar
    Dim jo As JavaObject = menu.Menus.Get(0)
    
    Dim lbl As Label
    lbl.Initialize("yourLabel")
    lbl.Text = "lol"
    jo.RunMethod("setGraphic",Array(lbl))

Thanks, that is very helpful!
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
@Enrique Gonzalez R thank you, that was exactly what I needed.

Full code below. It swaps an existing "title" menu item with a label with a font awesome icon that has a click event handler!

NOTE: to make the menu more "button like" it has no children; {Text: "replace-me",Children:[]}

1603092534747.png


B4X:
Sub ReplaceMenuItem
    Dim jo As JavaObject = MenuBar1.Menus.Get(4)
    Dim lbl As Label
    lbl.Initialize("yourLabel")
    lbl.Text = Chr(0xf0c5)
    lbl.Font = fx.CreateFontAwesome(18)
    jo.RunMethod("setText",Array("")) 'Replace existing text (if you have set some)
    jo.RunMethod("setGraphic",Array(lbl))
End Sub

Sub yourLabel_MouseClicked (EventData As MouseEvent)
    Log("Hello")
End Sub
 
Last edited:
Upvote 0
Top