Java Tutorial/SWT/MenuItem

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

Add Image to MenuItem

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
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 MenuItemWithImage {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Menu menuBar = new Menu(shell, SWT.BAR);
    Menu fileMenu = new Menu(menuBar);
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE | SWT.NO_RADIO_GROUP);
    fileItem.setText("File");
    fileItem.setMenu(fileMenu);
    Image yourImage = new Image(shell.getDisplay(), "yourFile.gif");
    MenuItem itema = new MenuItem(fileMenu, SWT.NONE);
    itema.setText("Radio A");
    itema.setImage(yourImage);
    MenuItem itemb = new MenuItem(fileMenu, SWT.NONE);
    itemb.setText("Radio B");
    MenuItem itemc = new MenuItem(fileMenu, SWT.NONE);
    itemc.setText("Radio C");
    shell.setMenuBar(menuBar);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





CheckBox Menu

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
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 CheckBoxMenuCreate {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    // Create the bar menu
    Menu menuBar = new Menu(shell, SWT.BAR);
    // Create the File item"s dropdown menu
    Menu fileMenu = new Menu(menuBar);
    // Create all the items in the bar menu
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
    fileItem.setText("File");
    fileItem.setMenu(fileMenu);
    // Create the second radio group
    MenuItem itema = new MenuItem(fileMenu, SWT.CHECK);
    itema.setText(" A");
    MenuItem itemb = new MenuItem(fileMenu, SWT.CHECK);
    itemb.setText(" B");
    MenuItem itemc = new MenuItem(fileMenu, SWT.CHECK);
    itemc.setText(" C");
    shell.setMenuBar(menuBar);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Create a radio menu item and deselects it

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
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 RadioMenuItemDeselected {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Menu menuBar = new Menu(shell, SWT.BAR);
    Menu fileMenu = new Menu(menuBar);
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE | SWT.NO_RADIO_GROUP);
    fileItem.setText("File");
    fileItem.setMenu(fileMenu);
    MenuItem itema = new MenuItem(fileMenu, SWT.RADIO);
    itema.setText("Radio A");
    itema.setSelection(true);
    
    MenuItem itemb = new MenuItem(fileMenu, SWT.RADIO);
    itemb.setText("Radio B");
    MenuItem itemc = new MenuItem(fileMenu, SWT.RADIO);
    itemc.setText("Radio C");
    shell.setMenuBar(menuBar);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Create menu items as part of a radio group using the SWT.RADIO style

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
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 RadioNoGroup {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Menu menuBar = new Menu(shell, SWT.BAR);
    Menu fileMenu = new Menu(menuBar);
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
    fileItem.setText("File");
    fileItem.setMenu(fileMenu);
    MenuItem itema = new MenuItem(fileMenu, SWT.RADIO);
    itema.setText("Radio A");
    MenuItem itemb = new MenuItem(fileMenu, SWT.RADIO);
    itemb.setText("Radio B");
    MenuItem itemc = new MenuItem(fileMenu, SWT.RADIO);
    itemc.setText("Radio C");
    shell.setMenuBar(menuBar);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Disable MenuItem

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
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 MenuItemDiabled {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Menu menuBar = new Menu(shell, SWT.BAR);
    Menu fileMenu = new Menu(menuBar);
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE | SWT.NO_RADIO_GROUP);
    fileItem.setText("File");
    fileItem.setMenu(fileMenu);
    MenuItem itema = new MenuItem(fileMenu, SWT.NONE);
    itema.setText("Radio A");
    itema.setEnabled(false);
    MenuItem itemb = new MenuItem(fileMenu, SWT.NONE);
    itemb.setText("Radio B");
    MenuItem itemc = new MenuItem(fileMenu, SWT.NONE);
    itemc.setText("Radio C");
    shell.setMenuBar(menuBar);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Set Accelerator for MenuItem

import org.eclipse.swt.SWT;
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 MenuItemAcceletorSetting {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Menu bar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(bar);
    MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
    fileItem.setText("&File");
    Menu submenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(submenu);
    MenuItem item = new MenuItem(submenu, SWT.PUSH);
    item.setText("Select &All\tCtrl+A");
    item.setAccelerator(SWT.MOD1 + "A");
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Set CheckBox Menu selected

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
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 CheckBoxSelected {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Menu menuBar = new Menu(shell, SWT.BAR);
    Menu fileMenu = new Menu(menuBar);
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE | SWT.NO_RADIO_GROUP);
    fileItem.setText("File");
    fileItem.setMenu(fileMenu);
    MenuItem itema = new MenuItem(fileMenu, SWT.CHECK);
    itema.setText("Radio A");
    itema.setSelection(true);
    
    MenuItem itemb = new MenuItem(fileMenu, SWT.NONE);
    itemb.setText("Radio B");
    MenuItem itemc = new MenuItem(fileMenu, SWT.NONE);
    itemc.setText("Radio C");
    shell.setMenuBar(menuBar);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Update a status line when an item is armed

/*******************************************************************************
 * 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
 *******************************************************************************/
/*
 * Control example snippet: update a status line when an item is armed
 *
 * 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.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
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 StatusLineUpdateWhenMenuItemArmed {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    
    FormLayout layout = new FormLayout();
    shell.setLayout(layout);
    
    final Label label = new Label(shell, SWT.BORDER);
    Listener armListener = new Listener() {
      public void handleEvent(Event event) {
        MenuItem item = (MenuItem) event.widget;
        label.setText(item.getText());
        label.update();
      }
    };
    Listener showListener = new Listener() {
      public void handleEvent(Event event) {
        Menu menu = (Menu) event.widget;
        MenuItem item = menu.getParentItem();
        if (item != null) {
          label.setText(item.getText());
          label.update();
        }
      }
    };
    Listener hideListener = new Listener() {
      public void handleEvent(Event event) {
        label.setText("");
        label.update();
      }
    };
    FormData labelData = new FormData();
    labelData.left = new FormAttachment(0);
    labelData.right = new FormAttachment(100);
    labelData.bottom = new FormAttachment(100);
    label.setLayoutData(labelData);
    Menu menuBar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(menuBar);
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
    fileItem.setText("File");
    fileItem.addListener(SWT.Arm, armListener);
    MenuItem editItem = new MenuItem(menuBar, SWT.CASCADE);
    editItem.setText("Edit");
    editItem.addListener(SWT.Arm, armListener);
    Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
    fileMenu.addListener(SWT.Hide, hideListener);
    fileMenu.addListener(SWT.Show, showListener);
    fileItem.setMenu(fileMenu);
    String[] fileStrings = { "New", "Close", "Exit" };
    for (int i = 0; i < fileStrings.length; i++) {
      MenuItem item = new MenuItem(fileMenu, SWT.PUSH);
      item.setText(fileStrings[i]);
      item.addListener(SWT.Arm, armListener);
    }
    Menu editMenu = new Menu(shell, SWT.DROP_DOWN);
    editMenu.addListener(SWT.Hide, hideListener);
    editMenu.addListener(SWT.Show, showListener);
    String[] editStrings = { "Cut", "Copy", "Paste" };
    editItem.setMenu(editMenu);
    for (int i = 0; i < editStrings.length; i++) {
      MenuItem item = new MenuItem(editMenu, SWT.PUSH);
      item.setText(editStrings[i]);
      item.addListener(SWT.Arm, armListener);
    }
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}