SWT Menu Example : Menu « SWT JFace Eclipse « Java






SWT Menu Example

SWT Menu Example

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class SWTMenuExample {

  Display display;

  Shell shell;

  Menu menuBar, fileMenu, helpMenu;

  MenuItem fileMenuHeader, helpMenuHeader;

  MenuItem fileExitItem, fileSaveItem, helpGetHelpItem;

  Label label;

  public SWTMenuExample() {

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

    label = new Label(shell, SWT.CENTER);
    label.setBounds(shell.getClientArea());

    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");

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

    helpMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
    helpMenuHeader.setText("&Help");

    helpMenu = new Menu(shell, SWT.DROP_DOWN);
    helpMenuHeader.setMenu(helpMenu);

    helpGetHelpItem = new MenuItem(helpMenu, SWT.PUSH);
    helpGetHelpItem.setText("&Get Help");

    fileExitItem.addSelectionListener(new fileExitItemListener());
    fileSaveItem.addSelectionListener(new fileSaveItemListener());
    helpGetHelpItem.addSelectionListener(new helpGetHelpItemListener());

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

  class fileExitItemListener implements SelectionListener {
    public void widgetSelected(SelectionEvent event) {
      shell.close();
      display.dispose();
    }

    public void widgetDefaultSelected(SelectionEvent event) {
      shell.close();
      display.dispose();
    }
  }

  class fileSaveItemListener implements SelectionListener {
    public void widgetSelected(SelectionEvent event) {
      label.setText("Saved");
    }

    public void widgetDefaultSelected(SelectionEvent event) {
      label.setText("Saved");
    }
  }

  class helpGetHelpItemListener implements SelectionListener {
    public void widgetSelected(SelectionEvent event) {
      label.setText("No worries!");
    }

    public void widgetDefaultSelected(SelectionEvent event) {
      label.setText("No worries!");
    }
  }

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


           
       








Related examples in the same category

1.SWT Menu and menu eventSWT Menu and menu event
2.Shared Menu in SWTShared Menu in SWT
3.Menu Examples Menu Examples
4.Demonstrates menusDemonstrates menus
5.Menu Shell
6.Menu Shell 2Menu Shell 2
7.Menu Shell 3Menu Shell 3
8.Menu Shell 4Menu Shell 4
9.Menu Shell 6Menu Shell 6
10.Menu Shell 5Menu Shell 5
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)