MenuItem: addListener(int state, Listener lis) : MenuItem « org.eclipse.swt.widgets « Java by API






MenuItem: addListener(int state, Listener lis)

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

public class MainClass {

  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(300, 200);

    ToolBar toolbar = new ToolBar(shell, SWT.NONE);
    ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
    item1.setText("Now");
    ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);
    item2.setText("is");
    ToolItem item3 = new ToolItem(toolbar, SWT.PUSH);
    item3.setText("the");
    ToolItem item4 = new ToolItem(toolbar, SWT.PUSH);
    item4.setText("time");

    toolbar.setBounds(0, 0, 200, 70);

    final Text text = new Text(shell, SWT.BORDER);
    text.setBounds(0, 100, 200, 25);

    Listener listener = new Listener() {
      public void handleEvent(Event event) {
        ToolItem item = (ToolItem) event.widget;
        String string = item.getText();
        if (string.equals("Now"))
          text.setText("You selected: Now");
        else if (string.equals("is"))
          text.setText("You selected: is");
        else if (string.equals("the"))
          text.setText("You selected: the");
        else if (string.equals("time"))
          text.setText("You selected: time");
      }
    };

    item1.addListener(SWT.Selection, listener);
    item2.addListener(SWT.Selection, listener);
    item3.addListener(SWT.Selection, listener);
    item4.addListener(SWT.Selection, listener);

    shell.open();

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

           
       








Related examples in the same category

1.new MenuItem(Menu menu, int style)
2.new MenuItem(Menu m, SWT.CASCADE)
3.new MenuItem(Menu optionsmenu, SWT.RADIO)
4.new MenuItem(MenuItem filemenu, SWT.SEPARATOR)
5.MenuItem: addSelectionListener(SelectionListener lis)
6.MenuItem: setAccelerator(int key)
7.MenuItem: setMenu(Menu menu)
8.MenuItem: setText(String s)