SWT Menu and menu event : Menu « SWT JFace Eclipse « Java






SWT Menu and menu event

SWT Menu and menu event

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;
import org.eclipse.swt.widgets.Text;

public class MenuClass {

  Display display;

  Shell shell;

  Menu menuBar, fileMenu, editMenu;

  MenuItem fileMenuHeader, editMenuHeader;

  MenuItem fileExitItem, fileSaveItem, fileEnglishItem, fileGermanItem,
      editCopyItem;

  Text text;

  public MenuClass() {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Menu Example");
    shell.setSize(300, 200);

    text = new Text(shell, SWT.BORDER);
    text.setBounds(80, 50, 150, 25);

    menuBar = new Menu(shell, SWT.BAR);
    fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
    fileMenuHeader.setText("&File");

    fileMenu = new Menu(shell, SWT.DROP_DOWN);
    fileMenuHeader.setMenu(fileMenu);

    fileSaveItem = new MenuItem(fileMenu, SWT.PUSH);
    fileSaveItem.setText("&Save");

    fileEnglishItem = new MenuItem(fileMenu, SWT.RADIO);
    fileEnglishItem.setText("English");

    fileGermanItem = new MenuItem(fileMenu, SWT.RADIO);
    fileGermanItem.setText("German");

    fileExitItem = new MenuItem(fileMenu, SWT.PUSH);
    fileExitItem.setText("E&xit");

    editMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
    editMenuHeader.setText("&Edit");

    editMenu = new Menu(shell, SWT.DROP_DOWN);
    editMenuHeader.setMenu(editMenu);

    editCopyItem = new MenuItem(editMenu, SWT.PUSH);
    editCopyItem.setText("&Copy");

    fileExitItem.addSelectionListener(new MenuItemListener());
    fileSaveItem.addSelectionListener(new MenuItemListener());
    editCopyItem.addSelectionListener(new MenuItemListener());

    fileEnglishItem.addSelectionListener(new RadioItemListener());
    fileGermanItem.addSelectionListener(new RadioItemListener());

    shell.setMenuBar(menuBar);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }

  class MenuItemListener extends SelectionAdapter {
    public void widgetSelected(SelectionEvent event) {
      if (((MenuItem) event.widget).getText().equals("E&xit")) {
        shell.close();
      }
      text.setText("You selected " + ((MenuItem) event.widget).getText());
    }
  }

  class RadioItemListener extends SelectionAdapter {
    public void widgetSelected(SelectionEvent event) {
      MenuItem item = (MenuItem) event.widget;
      text.setText(item.getText() + " is on.");
    }
  }

  public static void main(String[] args) {
    MenuClass menuExample = new MenuClass();
  }
}



           
       








Related examples in the same category

1.Shared Menu in SWTShared Menu in SWT
2.Menu Examples Menu Examples
3.Demonstrates menusDemonstrates menus
4.Menu Shell
5.Menu Shell 2Menu Shell 2
6.Menu Shell 3Menu Shell 3
7.Menu Shell 4Menu Shell 4
8.Menu Shell 6Menu Shell 6
9.Menu Shell 5Menu Shell 5
10.SWT Menu ExampleSWT Menu Example
11.Show a popup menu (wait for it to close)Show a popup menu (wait for it to close)
12.Fill a menu dynamically (when menu shown)Fill a menu dynamically (when menu shown)
13.Enable menu items dynamically (when menu shown)Enable menu items dynamically (when menu shown)
14.Create a menu with radio itemsCreate a menu with radio items
15.Create a popup menu (set in multiple controls)Create a popup menu (set in multiple controls)
16.Create a bar and pull down menu (accelerators, mnemonics)Create a bar and pull down menu (accelerators, mnemonics)