How to add buttons on sidemenu on top and bottom?

448 views Asked by At

enter image description hereI want to add two buttons on side menu . One at top and other at bottom so I have added below code but its not working and its displayed image is added . how to add buttons on sidemenu on top and bottom ?

enter image description here

Toolbar addToolbar(Form f) {


    Toolbar toolbar = new Toolbar();

        f.setToolbar(toolbar);

        Container container = new Container(new BorderLayout());
        container.add(BorderLayout.NORTH,new Button(" north"));
        container.add(BorderLayout.SOUTH,new Button(" south"));
        toolbar.addComponentToSideMenu(container);

    }


void addToolbars(Form fs) {
        Form f = new Form(" Test");
        Toolbar toolbar = new Toolbar();

        f.setToolbar(toolbar);

        Container container = new Container(new BorderLayout());
        container.add(BorderLayout.NORTH, new Button(" north"));
        container.add(BorderLayout.CENTER, new Button("center"));
        container.add(BorderLayout.SOUTH, new Button(" south"));
        toolbar.addComponentToSideMenu(container);

        Container mySideMenuContainer = new Container() {
            @Override
            public void initComponent() {

                getParent().setLayout(new GridLayout(1, 1));
                getParent().setScrollableY(false);
                getParent().revalidate();
            }
        };
        mySideMenuContainer.addComponent(container);
        toolbar.addComponentToSideMenu(mySideMenuContainer);
        f.show();
    }

the container contains only half of screen as shown in image

1

There are 1 answers

4
Shai Almog On

There is no API for that and it's not supported.

However, like anything in Codename One you can sometimes hack things together.

E.g. something like this should work as a hack although I can't guarantee it will work forever as it relies on implementation details of the addComponentToSideMenu method:

Container mySideMenuContainer = createSideMenuContainer() {
     @Override
     public void initComponent() {
        Container p = getParent();
        if(!(p.getLayout() instanceof BorderLayout)) {
            Component[] cmp = new Component[p.getComponentCount()];
            for(int iter = 0 ; iter < cmp.length ; iter++) {
               cmp[iter] = p.getComponentAt(iter);
            }
            p.removeAll();
            p.setLayout(new BorderLayout());
            if(cmp.length == 1) {
               p.add(BorderLayout.CENTER, cmp[0]);
            } else {
               p.add(BorderLayout.NORTH, cmp[0]);
               p.add(BorderLayout.CENTER, cmp[1]);
            }
            p.revalidate();
        }
     }
};
toolbar.addComponentToSideMenu(mySideMenuContainer);