B4J Question disable menu entry

madru

Active Member
Licensed User
Longtime User
Good morning,

must be blind but how can I do this, disable a menu entry?

THX


B4X:
MenuBar menuBar = new MenuBar();

Menu menu = new Menu("File");
MenuItem newItem = new MenuItem("Load");
menu.getItems().add(newItem);
menu.getItems().add(new MenuItem("Save"));
menu.getItems().add(new SeparatorMenuItem());
menu.getItems().add(new MenuItem("Quit"));

newItem.setDisable(true);
 

madru

Active Member
Licensed User
Longtime User
OK, found this here in the forum:

B4X:
    menus.Initialize
    CollectMenuItems(menus, Menubar1.Menus)
    Dim mi As MenuItem = menus.Get("Save")
    mi.Enabled = False

but unfortunately it doesn't work:

B4X:
Error occurred on line: 137 (Main) => 'mi.Enabled = False'
java.lang.RuntimeException: Object should first be initialized (MenuItem).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:32)
    at anywheresoftware.b4j.objects.MenuItemWrapper.setEnabled(MenuItemWrapper.java:91)
    at com.placr.scte.reader.main._appinit(main.java:259)
    at com.placr.scte.reader.main._appstart(main.java:385)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:593)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at com.placr.scte.reader.main.start(main.java:36)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

here is the menu structure:

B4X:
[
    {Text: "File",Children:
        [
            {Text: "Open",EventName: "mnuOpen",Tag: "mnuOpen",Shortcut: {Key: "o",Modifier: "CONTROL"}},
            {Text: "Save",EventName: "mnuSave",Tag: "mnuSave",Shortcut: {Key: "s",Modifier: "CONTROL"}},
            "-",
            {Text: "export HTML",EventName: "mnuExport",Tag: "mnuExport",Shortcut: {Key: "e",Modifier: "CONTROL"}},
            "-",
            {Text: "Quit",EventName: "mnuQuit",Tag: "mnuQuit",Shortcut: {Key: "x",Modifier: "CONTROL"}}
        ]
    },
    {Text: "Edit",Children:
        [
            {Text: "Copy",EventName: "mnuCopy",Tag: "mnuCopy",Shortcut: {Key: "c",Modifier: "CONTROL"}},
            {Text: "Past",EventName: "mnuPast",Tag: "mnuPast",Shortcut: {Key: "p",Modifier: "CONTROL"}},
            "-",
            {Text: "Clear",EventName: "mnuClear",Tag: "mnuClear",Shortcut: {Key: "c",Modifier: "CONTROL"}}
        ]
    },
    {Text: "Help",Children:
        [
            {Text: "About",EventName: "mnuAbout",Tag: "mnuAbout"}
        ]
    }
]

any idea whats wrong?

THX
 
Upvote 0

madru

Active Member
Licensed User
Longtime User
yes, you are absolutely right

B4X:
menus.Put(mi.Tag, mi)

I have overseen the 'Tag' ......

THX
 
Upvote 0
Top