B4J Question [SOLVED] Menubar dinamic loading question

rosippc64a

Active Member
Licensed User
Longtime User
Hello Everyone!
I use menubar in my B4J application. As it works for several companies, the menu structure/language may vary. I have created some subs to load the menu from file. The file contains the same text as it is in the menu designer. The subs are in the main. I thought I would move these subs to a class. The menu loads the same way, but does not respond to clicks. What do I need to do to get it right?
Here is the working code. If I move this code to a class, it will work, except the click event.
thanks in advance
Steven

Calling code:
    loadMenu("MenuBar1",MenuBar1,"menu.json")
Here is the whole code:
#region program menü betöltése fájlból

'A dirassetsben található menüleíró JSON fájlból felépíti a menüt.
'https://www.b4x.com/android/forum/threads/menu-manager.84401/#post-812776
public Sub loadMenu( pevent As String,main_menubar As MenuBar,fname As String )
    If File.Exists(File.DirApp,fname) Then
        Dim parser As JSONParser
        Try
            parser.Initialize(File.ReadString(File.DirApp,fname))
            Dim jroot As List = parser.NextArray
            For Each mp As Menu In main_menubar.Menus
                Log(mp.Text)
                mp.MenuItems.Clear
                'MenuBar1.RemoveNodeFromParent
            Next
            main_menubar.Menus.Clear
            'MenuBar1.Initialize("MP")
            parsemenu( main_menubar,jroot,pevent)
        Catch
            Log(LastException)
        End Try
    End If
End Sub
'Set a shortcut key for this menu item
'Returns the menu item
Private Sub SetShortCutKey(MI As JavaObject,Combination() As String) As MenuItem
    Dim KC As JavaObject
    KC.InitializeStatic("javafx.scene.input.KeyCombination")
    Dim KCS As String
    For i = 0 To Combination.Length - 1
        If i > 0 Then KCS = KCS & "+"
        KCS = KCS & Combination(i)
    Next
    MI.RunMethod("setAccelerator",Array(KC.RunMethod("keyCombination",Array(KCS))))
    Return MI
End Sub
Private Sub NewMenuItem(EventName As String,Text As String, pTag As String, KeyShortCut() As String) As MenuItem
    Dim MI As MenuItem
    MI.Initialize(Text,EventName)
    MI.Tag = pTag
    If KeyShortCut.Length > 0 Then
        SetShortCutKey(MI,KeyShortCut)
    End If
    Return MI
End Sub
private Sub parsemenu( menubar As Object, menujson As List, pevent As String ) As Object
    Try
        If menujson Is List Then
            Dim Text As String
            'a menujson tartalmaz helyes json elemeket és
            'egy közönséges "-"-t is, ami a menüseparator helyett áll.
            'ezért nem lehet az mjson map!
            For Each mjson As Object In menujson
                Try
                    If mjson Is Map Then
                        Dim coljroot As Map = mjson
                        Text = coljroot.Get("Text")
                        'Log(IIf(coljroot.Get("Children") Is List,"list","map"))
                        If coljroot.Get("Children") Is List Then
                            Dim FileM As Menu
                            FileM.Initialize(Text,"")
                            '(menubar).As(Menu).MenuItems.Add(parsemenu ( FileM, coljroot.Get("Children") ))
                            Dim resmenu As MenuItem = parsemenu ( FileM, coljroot.Get("Children"), pevent )
                            If menubar Is MenuBar Then
                                (menubar).As(MenuBar).Menus.Add(resmenu)
                            Else
                                (menubar).As(Menu).MenuItems.Add(resmenu)
                            End If
                        Else
                            Dim Tag As String = coljroot.Get("Tag")
                            Dim keys(1) As String = Array As String()
                            If coljroot.ContainsKey("Shortcut") Then
                                keys = Array As String(coljroot.get("Shortcut").As(Map).Get("Key").As(String))
                            End If
                            Dim mitem As MenuItem
                            mitem.Initialize(Text,Tag)
                            '(menubar).As(Menu).MenuItems.Add(MItem)
                            (menubar).As(Menu).MenuItems.Add(NewMenuItem(pevent,Text,Tag,keys))
                        End If
                    Else
                        Text = "-"
                        Dim TJO As JavaObject
                        TJO.InitializeNewInstance("javafx.scene.control.SeparatorMenuItem",Null)
                        (menubar).As(Menu).MenuItems.Add(TJO)
                    End If
                Catch
                    'Log(LastException)
                    Text = "-"
                    Dim TJO As JavaObject
                    TJO.InitializeNewInstance("javafx.scene.control.SeparatorMenuItem",Null)
                    (menubar).As(Menu).MenuItems.Add(TJO)
                End Try
                Log(Text)
            Next
        Else
            Log("Nem list")
        End If
    Catch
        Log(LastException)
    End Try
    Return menubar
End Sub
#End Region
 
Top