Java Tutorial/SWT/PopupMenu — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:44, 31 мая 2010

Add One Popup Menu to two different Controls

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ruposite;
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 PopupMenuAddTwoControls {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    
    Composite c1 = new Composite(shell, SWT.BORDER);
    c1.setSize(100, 100);
    
    Composite c2 = new Composite(shell, SWT.BORDER);
    c2.setBounds(100, 0, 100, 100);
    
    Menu menu = new Menu(shell, SWT.POP_UP);
    MenuItem item = new MenuItem(menu, SWT.PUSH);
    item.setText("Popup");
    
    c1.setMenu(menu);
    c2.setMenu(menu);
    
    shell.setMenu(menu);
    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





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();
      }
    }
  }
}





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.ruposite;
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();
  }
}





PopupMenu is not 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.ruposite;
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 PopupMenuComposite {
  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();
  }
}





Popup Radio MenuItem

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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 MenuItemPopupRadio {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Menu menu = new Menu(shell, SWT.POP_UP);
    for (int i = 0; i < 4; i++) {
      MenuItem item = new MenuItem(menu, SWT.RADIO);
      item.setText("Item " + i);
      item.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
          MenuItem item = (MenuItem) e.widget;
          if (item.getSelection()) {
            System.out.println(item + " selected");
          } else {
            System.out.println(item + " unselected");
          }
        }
      });
    }
    shell.setMenu(menu);
    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Show a popup menu (wait for it to close)

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

/*
 * Menu example snippet: show a popup menu (wait for it to close)
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.0
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class PopupMenuShow {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.addListener(SWT.MenuDetect, new Listener() {
      public void handleEvent(Event event) {
        Menu menu = new Menu(shell, SWT.POP_UP);
        MenuItem item = new MenuItem(menu, SWT.PUSH);
        item.setText("Menu Item");
        item.addListener(SWT.Selection, new Listener() {
          public void handleEvent(Event e) {
            System.out.println("Item Selected");
          }
        });
        menu.setLocation(event.x, event.y);
        menu.setVisible(true);
        while (!menu.isDisposed() && menu.isVisible()) {
          if (!display.readAndDispatch())
            display.sleep();
        }
        menu.dispose();
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}