Class ContextMenu

java.lang.Object
com.codename1.ui.ContextMenu

public final class ContextMenu extends Object

The right-click menu.

Codename One has carried the event for a long time -- Component#addContextMenuListener(ActionListener) fires on a secondary mouse button, a stylus barrel button or a long press -- and has never had anything that turns it into a menu. Every application that wanted one built its own popup, which is why none of them looked like the platform.

Two ways in. Give a component its commands and the menu appears by itself:

label.setContextMenuCommands(cut, copy, paste);

Or open it from a listener, when the commands depend on what was clicked:

table.addContextMenuListener(e -> {
    ContextMenu.show(table, e.getX(), e.getY(), commandsFor(rowAt(e.getY())));
    e.consume();
});

The menu is an anchored popup, which means it is drawn inside the application's surface rather than in a window of its own -- deliberately, and for the same reasons Dialog#showPopupDialog(Rectangle) gives: the rectangle it points at is in its host's coordinate space, a separate window would never receive the click meant to dismiss it, and it would steal focus from its opener every time it appeared. It is styled through PopupContentPane and Command, which the desktop native themes define.

A null or empty command array opens nothing. That is not a special case to work around: a menu with no items is a rectangle the user has to dismiss to learn it was empty.

  • Method Summary

    Modifier and Type
    Method
    Description
    static Command
    show(Component anchor, int x, int y, Command... commands)
    Opens the menu at a point, in the coordinate space of the anchor's top level.
    static Command
    show(Component anchor, Command... commands)
    Opens the menu over a component rather than at a point, for a caller that has no pointer position -- a keyboard menu key, or a disclosure button that opens the same menu a right click would.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • show

      public static Command show(Component anchor, int x, int y, Command... commands)

      Opens the menu at a point, in the coordinate space of the anchor's top level.

      Parameters
      • anchor: the component the menu belongs to; its top level hosts the popup

      • x: the pointer x coordinate

      • y: the pointer y coordinate

      • commands: the menu items, in order

      Returns

      the command the user chose, or null if the menu was dismissed or never opened

    • show

      public static Command show(Component anchor, Command... commands)

      Opens the menu over a component rather than at a point, for a caller that has no pointer position -- a keyboard menu key, or a disclosure button that opens the same menu a right click would.

      Parameters
      • anchor: the component to point at

      • commands: the menu items, in order

      Returns

      the command the user chose, or null