Codename One Back Command on left and Menu on Right

家住魔仙堡 提交于 2019-12-06 12:19:44

We supported a right side menu bar in the SideMenuBar but not in the Toolbar API. We support placing components/commands in the left/right side of the title area in the Toolbar API but not in the SideMenuBar.

I guess the solution is to add support for the right menu bar into the Toolbar API but I'm not sure what the complexities are for such a change.

I suggest filing an RFE in the issue tracker asking for this but it probably won't be soon as we are closing the features for 3.3 right now.

I have an app that does this. Search Google Play (or App Store) for "Torquepower Diesel Cummins Engine" app.

  1. in the theme Constants I set my own rightSideMenuImage and rightSideMenuPressImage, but the default hamburger menu may be OK for you.

  2. On the beforeXXXX of each form I do something like this:

    super.beforePartNumberForm(f);
    
    Toolbar tb = createToolbar(f);
    
    createBackCommand(f, tb);
    addHelpX(tb);
    addViewCartX(tb);
    addCallTorquepowerX(tb);
    addReverseSwipe(f);
    
  3. create the toolbar

    Toolbar createToolbar(Form f) {
    
        Toolbar tb = new Toolbar();
        f.setToolBar(tb);
    
        Label l = new Label();
        l.setIcon(res.getImage("tpd_logoZZ.png"));
    
        tb.setTitleComponent(l);
    
        return tb;
    }
    
  4. create the back button

    void createBackCommand(Form f, Toolbar tb) {
    
        Command c = new Command("") {
            @Override
            public void actionPerformed(ActionEvent evt) {
                back();
            }
        };
    
        c.setIcon(res.getImage("black_left_arrow-512.png"));
        c.setPressedIcon(res.getImage("grey_left_arrow-512.png"));
    
        // MUST set this before adding to toolbar, else get null pointer
        f.setBackCommand(c);
    
        tb.addCommandToLeftBar(c);
    }
    
  5. add whatever commands are needed to the sidemenu

    void addHelpX(Toolbar tb) {
        Command c = new Command("Help") {
            @Override
            public void actionPerformed(ActionEvent evt) {
                showForm("HelpForm", null);
            }
        };
    
        c.putClientProperty(SideMenuBar.COMMAND_PLACEMENT_KEY, SideMenuBar.COMMAND_PLACEMENT_VALUE_RIGHT);
        c.putClientProperty("SideComponent", new SideMenuItem(fetchResourceFile(), c.toString(), "very_basic_about.png"));
        c.putClientProperty("Actionable", Boolean.TRUE);
    
        tb.addCommandToSideMenu(c);
    
    }
    

I use my own SideMenuItem which is:

public class SideMenuItem extends Button {
    SideMenuItem() {
        this("");
    }

    SideMenuItem(String s) {
        super(s);
        setUIID("SideMenuItem");
        int h = Display.getInstance().convertToPixels(8, false);
        setPreferredH(h);
    }

    SideMenuItem(Resources res, String s, String icon) {
        super();
        setIcon(res.getImage(icon));
        setText(s);
        setUIID("SideMenuItem");
        int h = Display.getInstance().convertToPixels(8, false);
        setPreferredH(h);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!