MenuBar Error

30 views
Skip to first unread message

Rubén V

unread,
Nov 15, 2019, 9:10:51 PM11/15/19
to CodenameOne Discussions
Hi

I am trying to make use of a traditional menu that appears at the bottom of the form (not lateral or contextual).

The error that appears to me is:

java.lang.ClassCastException: com.mycia.menubar.MenuBar cannot be cast to com.codename1.ui.MenuBar

I appreciate if you provide me with an example to continue moving forward in my project.

My code:

public class MenuBar {

    private Form current;

    public void init(Object context) {
        try {
            // use two network threads instead of one
            updateNetworkThreadCount(2);
            Resources theme = Resources.openLayered("/theme");
            UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
            UIManager.getInstance().getLookAndFeel().setMenuBarClass(MenuBar.class);
        } catch (IOException ex) {
        }

    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }

        Image imSelect = null;
        Image imCancel = null;
        Image imMenu = null;
        try {
            imSelect = Image.createImage("/m_brasilia.png");
            imCancel = Image.createImage("/m_brasilia.png");
            imMenu = Image.createImage("/m_brasilia.png");
        } catch (IOException ex) {

        }
        //
        Form menuBar = new Form("Menu Prueba");

        menuBar.addCommand(new Command("Command 1", imSelect));
        menuBar.addCommand(new Command("Command 2", imCancel));
        menuBar.addCommand(new Command("Command 3", imMenu));

        menuBar.addCommandListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                Dialog.show("WARNING !", ((Command) evt.getSource()).getCommandName() + " pressed !", "OK", null);
            }
        });

        menuBar.show();
    }

    public void stop() {
        current = getCurrentForm();
    }

    public void destroy() {
    }
}

Shai Almog

unread,
Nov 15, 2019, 10:27:52 PM11/15/19
to CodenameOne Discussions
Hi,
what's the stack of the error?
I wouldn't call a class "MenuBar" as we have a public Codename One class with that exact name and imports might be problematic here...

I also wouldn't use commands for that. Most of the menus with the buttons in the bottom are really just tabs so for 90% of the cases you should just use tabs.
For the rest I would use a border layout form and place buttons in the south area to create these options.

Rubén V

unread,
Nov 16, 2019, 10:18:07 AM11/16/19
to CodenameOne Discussions
Ok, thanks for comment.
Reply all
Reply to author
Forward
0 new messages