Creating Pop-Up Menus

The last type of menu to examine is called the pop-up menu . A pop-up menu is one that appears at the current mouse cursor position whenever the user right-clicks (on Windows and most Unix systems) or Ctrl-clicks (on a Mac). Pop-up menus are often used to create context-sensitive menu choices when the user right-clicks a particular widget in a window. For example, if the cursor is located within the confines of a text widget, a right-click could pop up a menu giving instant access to Cut, Copy, and Paste functionality.

Note

Users love pop-up menus.

How do I do that?

Pop-up menus are easy to create and the steps are similar to those used to create a menu bar. In the case of a pop-up menu, the difference is that the highest-level menu has the style SWT.POP_UP rather than SWT.BAR. To create a pop-up of an Edit menu with Cut, Copy, and Paste menu items, you would use the following:

private Menu createEditPopup( ){ final Menu p = new Menu(s,SWT.POP_UP); final MenuItem cutItem = new MenuItem(p, SWT.PUSH); cutItem.setText("&Cut"); final MenuItem copyItem = new MenuItem(p, SWT.PUSH); copyItem.setText("Co&py"); final MenuItem pasteItem = new MenuItem(p, SWT.PUSH); pasteItem.setText("&Paste"); cutItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent e) { System.out.println("Cut"); } public void widgetDefaultSelected(SelectionEvent e) { } }); copyItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent ...

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.