Codename One open side menu programatically

I'm trying to make the side menu open on my app from code.
My form uses the toolbar to populate the side menu with more complicated components.
I've used this to close the menu:

SideMenuBar.closeCurrentMenu();

but I can't find the equivalent open function.
I attempted this:

        if(f.getToolbar() !=null){
            MenuBar mb = f.getToolbar().getMenuBar();
            if(mb!=null){
                mb.showMenu();
            }    
        }

but It is throwing a null pointer because it can't find any commands (Toolbar.java line 490)

stack trace:

java.lang.NullPointerException
at java.util.ArrayList.<init>(ArrayList.java:177)
at com.codename1.ui.list.DefaultListModel.<init>(DefaultListModel.java:65)
at com.codename1.ui.List.<init>(List.java:275)
at com.codename1.ui.Toolbar.createOverflowCommandList(Toolbar.java:490)
at com.codename1.ui.Toolbar$ToolbarSideMenu.createCommandComponent(Toolbar.java:799)
at com.codename1.ui.MenuBar.showMenu(MenuBar.java:692)
at com.startex.coffee.managers.forms.MyCardsController.lambda$setupButtonActions$5(MyCardsController.java:217)
at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:455)
at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:358)
at com.codename1.ui.Button.fireActionEvent(Button.java:411)
at com.codename1.ui.Button.released(Button.java:442)
at com.codename1.ui.Button.pointerReleased(Button.java:530)
at com.codename1.ui.Form.pointerReleased(Form.java:2578)
at com.codename1.ui.Form.pointerReleased(Form.java:2514)
at com.codename1.ui.Component.pointerReleased(Component.java:3119)
at com.codename1.ui.Display.handleEvent(Display.java:2017)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1065)
at com.codename1.ui.Display.mainEDTLoop(Display.java:994)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

Upvotes: 0

Views: 693

Answers (2)

Gabriel Haas
Gabriel Haas

Reputation: 117

The SideMenuBar has the non-static method openMenu(String direction). To open a normal SideMenuBar you can proceed like this:

SideMenuBar smb = (SideMenuBar) Display.getInstance().getCurrent().getMenuBar();
smb.openMenu(null);

I suppose in your case f is the Form and since you are using the ToolBar you have to call getMenuBar() on getToolbar() and then cast it to a SideMenuBar as folowing:

((SideMenuBar)f.getToolbar().getMenuBar()).openMenu(null);

Please note that the openMenu() method has a direction parameter, which I'm not entirely sure what it does. For me it works with null. I think it is needed if you don't have the menu on the left side, for that this link might help.

Upvotes: 3

tizbn
tizbn

Reputation: 1907

  1. The nullpointer is due to empty overflow commands so add some commands to overflowmenu and the null pointer is solved and it shows overflowmenu

    Toolbar toolbar = new Toolbar(); f.setToolbar(toolbar);
    toolbar.addCommandToOverflowMenu(new Command("hi hi "));

  2. To show sidemenu add the following code

     SideMenuBar sideMenuBar= (SideMenuBar)Display.getInstance().getCurrent().getMenuBar();
     sideMenuBar.openMenu(null);
    

Upvotes: 1

Related Questions