PopupMenu is disposed with the composite : PopupMenu « SWT « Java Tutorial






PopupMenu is disposed with the composite
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
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 PopupComposite {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);

    shell.setLayout(new GridLayout(2, true));

    final Composite composite1 = new Composite(shell, SWT.BORDER);
    composite1.setBackground(display.getSystemColor(SWT.COLOR_BLACK));

    final Composite composite2 = new Composite(shell, SWT.BORDER);
    composite2.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

    Menu menu = new Menu(composite1);
    MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
    menuItem.setText("Popup menu");

    menuItem.addDisposeListener(new DisposeListener() {
      public void widgetDisposed(DisposeEvent e) {
        System.out.println("Menu item is disposed.");
      }
    });

    menuItem.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        System.out.println("Dispsoing ...");
        //composite2.setMenu(null);
        composite2.dispose();
      }
    });

    composite1.setMenu(menu);
    composite2.setMenu(menu);

    shell.pack();
    shell.open();

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }

    display.dispose();
  }
}








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