Creating a Popup Menu : PopupMenu « SWT « Java Tutorial






Creating a Popup Menu
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class PopupMenuCreate {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());

    Button bn = new Button(shell, SWT.FLAT);
    bn.setText("Right Click to see the popup menu");
    
    Menu popupMenu = new Menu(bn);
    MenuItem newItem = new MenuItem(popupMenu, SWT.CASCADE);
    newItem.setText("New");
    MenuItem refreshItem = new MenuItem(popupMenu, SWT.NONE);
    refreshItem.setText("Refresh");
    MenuItem deleteItem = new MenuItem(popupMenu, SWT.NONE);
    deleteItem.setText("Delete");

    Menu newMenu = new Menu(popupMenu);
    newItem.setMenu(newMenu);

    MenuItem shortcutItem = new MenuItem(newMenu, SWT.NONE);
    shortcutItem.setText("Shortcut");
    MenuItem iconItem = new MenuItem(newMenu, SWT.NONE);
    iconItem.setText("Icon");

    bn.setMenu(popupMenu);
    
    
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}








17.34.PopupMenu
17.34.1.Creating a Popup MenuCreating a Popup Menu
17.34.2.Show a popup menu (wait for it to close)Show a popup menu (wait for it to close)
17.34.3.Popup Radio MenuItemPopup Radio MenuItem
17.34.4.Add One Popup Menu to two different ControlsAdd One Popup Menu to two different Controls
17.34.5.PopupMenu is disposed with the compositePopupMenu is disposed with the composite
17.34.6.PopupMenu is not disposed with the compositePopupMenu is not disposed with the composite