B4J Question how to make a MenuBar ?

giannimaione

Well-Known Member
Licensed User
Longtime User
how to make from code a MenuBar without internal designer?
 

Makumbi

Well-Known Member
Licensed User
how to make from code a MenuBar without internal designer?
 
Upvote 0

giannimaione

Well-Known Member
Licensed User
Longtime User
at Makumbi,
not work.
there is already a Menu Bar created from Designer, and after will insert a Icon Font Awesome.
i want start from a "blank" Menu Bar
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
MenuBar is just another node so you can add it like any other. You will have to manage the width in the form resize event.

If you really don't want to use the designer this will work:

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

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MB.Initialize("MB")
    MainForm.RootPane.AddNode(MB,0,0,MainForm.RootPane.PrefWidth,20)
    MainForm.Show
  
    Dim FileM As Menu
    FileM.Initialize("File","Menu")
  
    Dim FileOpen As MenuItem
    FileOpen.Initialize("Open","Menu")
    FileM.MenuItems.Add(FileOpen)
  
    MB.Menus.Add(FileM)
End Sub

Private Sub MainForm_Resize (Width As Double, Height As Double)
    MB.PrefWidth = Width
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

Private Sub Menu_Action
    Dim MI As MenuItem = Sender
    Select MI.Text
        Case "Open"
            Log("Open Menu Action")
    End Select
End Sub
 
Last edited:
Upvote 0

giannimaione

Well-Known Member
Licensed User
Longtime User
ok stevel05,
now i can create this:
FILE ; REPORT ; EXPORT ; ABOUT (horizontal)
and in vertical
FILE
Open
Close
Exit

but how create a Children and sub Children
 
Upvote 0

giannimaione

Well-Known Member
Licensed User
Longtime User
Why aren't you using the designer to do it?
my idea;
i want create a dynamic menu; each items / element of MenuBar is stored in a db (sqlite, mysql) ; into db are store users/groups too; each user or groups have a personal menu or able to click/select a some items of menu.

i have already make another APP with menubar from designer; it is good!
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
but how create a Children and sub Children

Just add a Menu to the parent instead of a MenuItem like this:

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MB.Initialize("MB")
    MainForm.RootPane.AddNode(MB,0,0,MainForm.RootPane.PrefWidth,20)
    MainForm.Show
    
    Dim FileM As Menu
    FileM.Initialize("File","Menu")
    
    Dim FileOpen As MenuItem
    FileOpen.Initialize("Open","Menu")
    FileM.MenuItems.Add(FileOpen)
    
    Dim SubMenu1 As Menu
    SubMenu1.Initialize("Sub1","Menu")
    
    Dim SubItem1 As MenuItem
    SubItem1.Initialize("SubItem1","Menu")
    SubMenu1.MenuItems.Add(SubItem1)
    
    FileM.MenuItems.Add(SubMenu1)
    
    MB.Menus.Add(FileM)
End Sub

Private Sub Menu_Action
    Dim MI As MenuItem = Sender
    Select MI.Text
        Case "Open"
            Log("Open Menu Action")
        Case "SubItem1"
            Log("Sub Item1 Action")
            
    End Select
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
In case it's useful to you, you can add menu separators like this:

B4X:
Private Sub MenuSeparatorItem As Object
    Dim TJO As JavaObject
    TJO.InitializeNewInstance("javafx.scene.control.SeparatorMenuItem",Null)
    Return TJO
End Sub

And using
B4X:
    FileM.MenuItems.AddAll(Array(MenuSeparatorItem, SubMenu1))
 
Last edited:
Upvote 0

Similar Threads

Top