public final class ContextMenu
- Object
- ContextMenu
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.
Methods
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. |
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. |
Inherited methods
Method details
show
public static Command show(Component anchor, int x, int y, Command... commands)Parameters
anchorComponent- the component the menu belongs to; its top level hosts the popup
xint- the pointer x coordinate
yint- the pointer y coordinate
commandsCommand...- the menu items, in order
Returns
show
public static Command show(Component anchor, Command... commands)Parameters
anchorComponent- the component to point at
commandsCommand...- the menu items, in order