B4J Question Menubar

mrossen

Active Member
Licensed User
Longtime User
Hi,

I would like create a sub menu where I use Check Menu Items.

Something like this:

Settings
ComPort->
Com1
Com2​


My plan was to make a submenu where I create Check Menu Items for the serial ports available,

I can not figure the menu system out. Anyone know how to make this?

I tried to do some thing like this.
B4X:
Private mnuComPort As Menu
    mnuComPort.MenuItems.CheckMenuItems.AddAll(Serial1.ListPorts)

Mogens
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot add strings as menu items.

SS-2013-12-18_15.00.55.png


This code will help you get started:
B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
   Dim mb As MenuBar
   mb.Initialize("")
   MainForm.RootPane.AddNode(mb, 0, 0, 0,0)
   Dim ports As Menu
   ports.Initialize("Ports", "")
   Dim FileMenu As Menu
   FileMenu.Initialize("FileMenu", "")
   mb.Menus.Add(FileMenu)
   FileMenu.MenuItems.Add(ports)
   For Each m As String In Array As String("a", "b", "c")
     Dim mm As MenuItem
     mm.Initialize(m, "mm")
     ports.MenuItems.Add(mm)
   Next
End Sub

Sub mm_Action
   Dim mi As MenuItem = Sender
   Log(mi.Text)
End Sub
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi,

Now I maybe know why I am a little confused.

I have taken this part from your code:

B4X:
For Each m As String In Array As String("a", "b", "c")
    Dim mm As MenuItem
    mm.Initialize(m, "mm")
    mnuComPort.MenuItems.Add(mm)
    Next

and put into mine.

This works only in "Release" not "Debug"

In debug:


Program started.
WARNING: com.sun.javafx.css.StyleHelper calculateValue Could not resolve '-fx-text-base-color' while resolving lookups for '-fx-text-fill' from rule '*.combo-box *.list-cell' in stylesheet jar:file:/C:/Program%20Files%20(x86)/Java/jdk1.7.0_45/jre/lib/jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.bss
Error occurred on line: 95 (main).
java.lang.RuntimeException: Method: getMenuItems not found in: anywheresoftware.b4j.objects.MenuItemWrapper
at anywheresoftware.b4a.shell.Shell$MethodCache.getMethod(Shell.java:718)
at anywheresoftware.b4a.shell.Shell.getMethod(Shell.java:381)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:467)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:209)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:153)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:69)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at lp.diagnostics.main.start(main.java:33)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)

Mogens
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
No, that works perfect :)

I have made the Menubar, Settings menu and ComPort menu with Scene Builder. Can this be the problem.
I think it is strange it works in release and not debug.

B4X:
    For Each m As String In Serial1.ListPorts
    Dim mm As CheckMenuItem
    mm.Initialize(m, "mm")
    mnuComPort.MenuItems.Add(mm)
    Next

I got this to work thanks to you :) See attached dumb

Mogens
 

Attachments

  • diag.jpg
    diag.jpg
    130 KB · Views: 462
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi,

I have another problem with this,

I would like to store the chosen com port in a settings file. I can do that when I click on the comport in the menu.

But when the program start again I have to set com port number stored in the settings file

I can :
B4X:
For Each m As String In Serial1.ListPorts
    Dim mm As CheckMenuItem
    mm.Initialize(m, "mm")
    mnuComPort.MenuItems.Add(mm)
    Next
  
    mm.Selected = True

But it just select the last in the array.

How do I find the one stored in my settings file and select it, and unselect if other is selected ?

Mogens
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I have made the Menubar, Settings menu and ComPort menu with Scene Builder. Can this be the problem.
Yes. There is a known issue in v1.06 with nested menu items. It is fixed for the next update.

B4X:
For Each m As String In Serial1.ListPorts
    Dim mm As CheckMenuItem
    mm.Initialize(m, "mm")
    mnuComPort.MenuItems.Add(mm)
    If m = ValueLoadedFromFile Then mm.Selected = True
   Next
 
Upvote 0
Top