B4J Question Contextmenu (SOLVED)

davelew1s

Active Member
Licensed User
Longtime User
Can anyone give me an example of how to add a contextmenu to a Label. It's probably easy but I cannot get it to work.
I have searched the forum but nothing helped.
Thanks Dave.
 

teddybear

Well-Known Member
Licensed User
Can anyone give me an example of how to add a contextmenu to a Label. It's probably easy but I cannot get it to work.
I have searched the forum but nothing helped.
Thanks Dave.

Try this code

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Label1 As Label
    Private ctxMenu As ContextMenu
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    ctxMenu.Initialize("")
    Dim MT() As String = Array As String("MI1","MI2","MI3")
    For i = 0 To MT.Length - 1
        Dim Mi As MenuItem
        Mi.Initialize(MT(i),"ctxm")
        ctxMenu.MenuItems.Add(Mi)
    Next
End Sub
Private Sub Label1_MouseClicked (E As MouseEvent)
    Dim jo As JavaObject = ctxMenu
    Dim ScreenX As Double = E.As(JavaObject).RunMethod("getScreenX", Null)
    Dim ScreenY As Double = E.As(JavaObject).RunMethod("getScreenY", Null)
    jo.RunMethod("show",Array(Label1,ScreenX,ScreenY))
End Sub
Sub ctxm_Action
    Dim Mi As MenuItem = Sender
    Log(Mi.Text)
End Sub
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You can set the contextmenu directly on the label, which saves the mouse clicked sub and Ctxmenu doesn't need to be a global variable as it is referenced in the Label Object.

Sorry, I stole the rest of your code :)

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Label1 As Label

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
 
    Dim ctxMenu As ContextMenu
    ctxMenu.Initialize("")
    Dim MT() As String = Array As String("MI1","MI2","MI3")
    For i = 0 To MT.Length - 1
        Dim Mi As MenuItem
        Mi.Initialize(MT(i),"ctxm")
        ctxMenu.MenuItems.Add(Mi)
    Next
 
    Label1.As(JavaObject).RunMethod("setContextMenu",Array(ctxMenu))
 
End Sub

Sub ctxm_Action
    Dim Mi As MenuItem = Sender
    Log(Mi.Text)
End Sub
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It's as simple as adding a Menu object instead of a MenuItem object:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Label1 As Label

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
 
    Dim ctxMenu As ContextMenu
    ctxMenu.Initialize("")
    Dim MT() As String = Array As String("MI1","MI2","MI3")
    For i = 0 To MT.Length - 1
        Dim Mi As MenuItem
        Mi.Initialize(MT(i),"ctxm")
        ctxMenu.MenuItems.Add(Mi)
    Next
 
    'Add a sub menu

    Dim SubMenu As Menu
    SubMenu.Initialize("Sub Menu1","")
 
    Dim SMT() As String = Array As String("SMI1","SMI2","SMI3")
 
    For i = 0 To SMT.Length - 1
        Dim Mi As MenuItem
        Mi.Initialize(SMT(i),"ctxm")
        SubMenu.MenuItems.Add(Mi)
    Next
 
    ctxMenu.MenuItems.Add(SubMenu)
 
    Label1.As(JavaObject).RunMethod("setContextMenu",Array(ctxMenu))
 
End Sub

Sub ctxm_Action
    Dim Mi As MenuItem = Sender
    Log(Mi.Text)
End Sub
 
Last edited:
Upvote 0

davelew1s

Active Member
Licensed User
Longtime User
It's as simple as adding a Menu object instead of a MenuItem object:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Label1 As Label

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
 
    Dim ctxMenu As ContextMenu
    ctxMenu.Initialize("")
    Dim MT() As String = Array As String("MI1","MI2","MI3")
    For i = 0 To MT.Length - 1
        Dim Mi As MenuItem
        Mi.Initialize(MT(i),"ctxm")
        ctxMenu.MenuItems.Add(Mi)
    Next
 
    'Add a sub menu

    Dim SubMenu As Menu
    SubMenu.Initialize("Sub Menu1","")
 
    Dim SMT() As String = Array As String("SMI1","SMI2","SMI3")
 
    For i = 0 To SMT.Length - 1
        Dim Mi As MenuItem
        Mi.Initialize(SMT(i),"ctxm")
        SubMenu.MenuItems.Add(Mi)
    Next
 
    ctxMenu.MenuItems.Add(SubMenu)
 
    Label1.As(JavaObject).RunMethod("setContextMenu",Array(ctxMenu))
 
End Sub

Sub ctxm_Action
    Dim Mi As MenuItem = Sender
    Log(Mi.Text)
End Sub
Thanks Steve
It's as simple as adding a Menu object instead of a MenuItem object:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Label1 As Label

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
 
    Dim ctxMenu As ContextMenu
    ctxMenu.Initialize("")
    Dim MT() As String = Array As String("MI1","MI2","MI3")
    For i = 0 To MT.Length - 1
        Dim Mi As MenuItem
        Mi.Initialize(MT(i),"ctxm")
        ctxMenu.MenuItems.Add(Mi)
    Next
 
    'Add a sub menu

    Dim SubMenu As Menu
    SubMenu.Initialize("Sub Menu1","")
 
    Dim SMT() As String = Array As String("SMI1","SMI2","SMI3")
 
    For i = 0 To SMT.Length - 1
        Dim Mi As MenuItem
        Mi.Initialize(SMT(i),"ctxm")
        SubMenu.MenuItems.Add(Mi)
    Next
 
    ctxMenu.MenuItems.Add(SubMenu)
 
    Label1.As(JavaObject).RunMethod("setContextMenu",Array(ctxMenu))
 
End Sub

Sub ctxm_Action
    Dim Mi As MenuItem = Sender
    Log(Mi.Text)
End Sub
Thanks Steve105 that works great ... i did try something like this but it didn't work.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Just out of interest where did you find this information
I'm not sure now, I've been using it for a long time.

If I can't find information on anything I need on the forum, I usually search google for a JavaFX solution, then convert it to B4x. Which is not a difficult process.
 
Upvote 0
Top