B4J Question MenuButton

Nokia

Active Member
Licensed User
Longtime User
I Have adding a MenuButton to my app, however I'm having a bit of a problem with adding menu items and the event name.

B4X:
Private bmVariable As JavaObject
bmVariable.InitializeNewInstance("javafx.scene.control.MenuButton",Array As Object("V"))

apMain.AddNode(bmVariable,57,10,40,25)
 
Solution
Solved, code below.

B4X:
Sub Class_Globals
Private fx As JFX

Private bmVariable As JavaObject

end sub

Public Sub Initialize()

    'Menu Button 1       
    bmVariable.InitializeNewInstance("javafx.scene.control.MenuButton",Array As Object("V"))

    AddMenuItem("Menu Item 1")
end sub

Private Sub AddMenuItem(MenuText As String)
    Private TheMenuItem As MenuItem
    TheMenuItem.Initialize(MenuText,"miTA_Variables")
    
    Dim miJO As JavaObject = TheMenuItem
    bmVariable.RunMethodJO("getItems", Null).RunMethod("add", Array(miJO))
End Sub

Sub miTA_Variables_Action
    Private meit As MenuItem = Sender
    Log("Menu Item: " & meit.Text)
End Sub

Brian Dean

Well-Known Member
Licensed User
Longtime User
. . . a problem with adding menu items and the event name.
Add menu items in the Form Designer. First add a "MenuBar" view - then find "Menu Items" at the bottom of its Properties list and click on the ellipsis ". . .". Add the menu items as a JSON object like this ...
B4X:
[
    {Text: "File", Children:
        [
            "New", "Open ...", "Save", "Save As ...",
            "-",
            "Exit"
        ]
    },
    {Text: "Help", Children:
        [    "Show help ..."
        ]
    }
]

In your Main code module add an event handler . . .
B4X:
Sub mnuMain_Action
    Dim item As MenuItem = Sender
    If (item.Text.EqualsIgnoreCase("open ...")) Then
        openScript
        previewScript
    End If
    If (item.Text.EqualsIgnoreCase("save")) Then saveScript(False)
    If (item.Text.EqualsIgnoreCase("save as ...")) Then saveScript(True)
    If (item.Text.EqualsIgnoreCase("exit")) Then MainForm.Close
    If (item.Text.EqualsIgnoreCase("show help ...")) Then showHelp
End Sub
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
I was able to get the menu itmes to add.

B4X:
    Dim miJO As JavaObject
    miJO.InitializeNewInstance("javafx.scene.control.MenuItem", Array As Object(MenuText))
    bmVariable.RunMethodJO("getItems", Null).RunMethod("add", Array(miJO))

now trying to add event. but get this error: java.lang.RuntimeException: Method: setOnAction not matched.

B4X:
miJO.RunMethod("setOnAction", Array("miTA_Variables"))
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
I have also tried this with no luck:

B4X:
Private Sub AddMenuItem(MenuText As String)

    Dim miJO As JavaObject
    miJO.InitializeNewInstance("javafx.scene.control.MenuItem", Array As Object(MenuText))
    
    Dim mieJO As Object = miJO.CreateEventFromUI("javafx.event.EventHandler", "miTA_Variables_Action", Null)
    miJO.RunMethod("setOnAction", Array(mieJO))
    
    bmVariable.RunMethodJO("getItems", Null).RunMethod("add", Array(miJO))

End Sub

Sub miTA_Variables_Action
    Private meit As MenuItem = Sender
    Log("Menu Item: " & meit.Text)
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
What's the benefits all those Java object run method compared to the b4j menu properties?
Menu has only one event to which the clicked item called is passed. Items are added, either by code or in the designer, by passing a json string to it...
I really don't understand why it's so troublesome to you.
Have you done a simple search about menu view?

One result that will surely enlighten you.
Thread 'Designer Menu Items' https://www.b4x.com/android/forum/threads/designer-menu-items.56602/

Other results are surely worth reading...
 
Last edited:
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
What's the benefits all those Java object run method compared to the b4j menu properties?
Menu has only one event to which the clicked item called is passed. Items are added, either by code or in the designer, by passing a json string to it...
I really don't understand why it's so troublesome to you.
Have you done a simple search about menu view?

One result that will surely enlighten you.
Thread 'Designer Menu Items' https://www.b4x.com/android/forum/threads/designer-menu-items.56602/

Other results are surely worth reading...

I'm adding a "Menubutton" to which I don't see any support for from B4J. So I have to add it through code, I'm Just stuck now on setting the event name for menu item so when someone clicks an item I can do what I need to do..
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I'm adding a "Menubutton" to which I don't see any support for from B4J
Of course menus and menu buttons are supported in B4J. Where are you trying to add this menu button? Is it not on a menubar?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I'm adding a "Menubutton" to which I don't see any support for from B4J. So I have to add it through code, I'm Just stuck now on setting the event name for menu item so when someone clicks an item I can do what I need to do..
Just browse the links I posted, all is there!!!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
Of course menus and menu buttons are supported in B4J. Where are you trying to add this menu button? Is it not on a menubar?

you can't declare a "menubutton" (at lease in the version of b4j I have. 9.30). I am adding to a Pane in a certain location. In javaFX Scene Builder 2.0 you can add anywhere. not adding to any menu bar.

I am able to add menubutton with no problem, the probem I am having to giving an event name so that I can process when user clicks a menu item.
 

Attachments

  • Screenshot 2024-01-27 123304.png
    Screenshot 2024-01-27 123304.png
    20.5 KB · Views: 74
Upvote 0

Nokia

Active Member
Licensed User
Longtime User

Attachments

  • Screenshot 2024-01-27 123304.png
    Screenshot 2024-01-27 123304.png
    20.5 KB · Views: 71
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
you can't declare a "menubutton" (at lease in the version of b4j I have. 9.30). I am adding to a Pane in a certain location. In javaFX Scene Builder 2.0 you can add anywhere. not adding to any menu bar.

I am able to add menubutton with no problem, the probem I am having to giving an event name so that I can process when user clicks a menu item.
Again... don't reinvent the wheel, @stevel05 created the menubutton lib exactly for that special case were what you need is just a menubutton and not a menubar...
The link is in that post you just quoted
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
Again... don't reinvent the wheel, @stevel05 created the menubutton lib exactly for that special case were what you need is just a menubutton and not a menubar...
The link is in that post you just quoted

I did see that, but I'm 98% of the way already and all I need to know now is how to add the event string/name to each menu item, so I can process the click (_Action).

B4X:
    Private TheMenuItem As MenuItem
    TheMenuItem.Initialize(MenuText,"miTA_Variables")
    
Sub miTA_Variables_Action
    Private meit As MenuItem = Sender
    fx.Clipboard.SetString(meit.Text)
    
    Log("Menu Item: " & meit.Text)
    
End Sub

Doing it this way helps me learn the java side and how it works with b4j. It also helps me learn how to apply to other java objects that are not directly supported by b4j.

don't get me wrong, I love to take shortcuts.

I have to thank you, so in typing this, I solved my own Problem.

B4X:
Private Sub AddMenuItem(MenuText As String)
    Private TheMenuItem As MenuItem
    TheMenuItem.Initialize(MenuText,"miTA_Variables")
    
    Dim miJO As JavaObject = TheMenuItem
    bmVariable.RunMethodJO("getItems", Null).RunMethod("add", Array(miJO))
End Sub

Sub miTA_Variables_Action
    Private meit As MenuItem = Sender
    fx.Clipboard.SetString(meit.Text)
    
    Log("Menu Item: " & meit.Text)
End Sub
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
Solved, code below.

B4X:
Sub Class_Globals
Private fx As JFX

Private bmVariable As JavaObject

end sub

Public Sub Initialize()

    'Menu Button 1       
    bmVariable.InitializeNewInstance("javafx.scene.control.MenuButton",Array As Object("V"))

    AddMenuItem("Menu Item 1")
end sub

Private Sub AddMenuItem(MenuText As String)
    Private TheMenuItem As MenuItem
    TheMenuItem.Initialize(MenuText,"miTA_Variables")
    
    Dim miJO As JavaObject = TheMenuItem
    bmVariable.RunMethodJO("getItems", Null).RunMethod("add", Array(miJO))
End Sub

Sub miTA_Variables_Action
    Private meit As MenuItem = Sender
    Log("Menu Item: " & meit.Text)
End Sub
 
Upvote 0
Solution
Top