ContextMenu

The ContextMenu is a LWUIT component. It is similar to the platform menu. It allows the developer to create and show the menu in the most suitable way for the MIDlet. The ContextMenu extends from dialog therefore the API is very similar.

To have the same behavior as the platform menu, the developer should use the LONG_TAP gesture to show the menu. The easiest way to use the ContextMenu is to call the static show() method which will return the selected command.

Figure 1. ContextMenu in a MIDlet

Implementation

  1. Create an instance of Button and addActionlistener, and add it on to a Form.

    show = new Button("Show ContextMenu");
    show.addActionListener(al);
    myForm.addComponent(show);
    
  2. Create ActionListener for Button.

    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (evt.getSource() == show) {
                ...
                ...
            }
        }
    };
  3. Call ContextMenu.show() method inside actionPerformed(ActionEvent) to show ContextMenu on Button tap.

    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == show) {
            Command pressedMenuItem = null;
            Command[] cmd = new Command[6];
            cmd[0] = new Command("Menu item one");
            cmd[1] = new Command("Menu item two");
            cmd[2] = new Command("Menu item three");
            cmd[3] = new Command("Menu item four");
            cmd[4] = new Command("Menu item five");
            cmd[5] = new Command("Menu item six");
            pressedMenuItem = ContextMenu.show(cmd, show);
        }
    }

Last updated 24 June 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×