Java/SWT JFace Eclipse/Menu
Содержание
- 1 Create a bar and pull down menu (accelerators, mnemonics)
- 2 Create a menu with radio items
- 3 Create a popup menu (set in multiple controls)
- 4 Demonstrates menus
- 5 Enable menu items dynamically (when menu shown)
- 6 Fill a menu dynamically (when menu shown)
- 7 Menu Examples
- 8 Menu Shell
- 9 Menu Shell 2
- 10 Menu Shell 3
- 11 Menu Shell 4
- 12 Menu Shell 5
- 13 Menu Shell 6
- 14 Shared Menu in SWT
- 15 Show a popup menu (wait for it to close)
- 16 SWT Menu and menu event
- 17 SWT Menu Example
/*
* Menu example snippet: create a bar and pull down menu (accelerators, mnemonics)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
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 Snippet29 {
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.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
System.out.println("Select All");
}
});
item.setText("Select &All\tCtrl+A");
item.setAccelerator(SWT.CTRL + "A");
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
/*
* Menu example snippet: create a menu with radio items
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
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 Snippet89 {
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();
}
}
/*
* Menu example snippet: create a popup menu (set in multiple controls)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
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 Snippet40 {
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();
}
}
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This class demonstrates menus
*/
public class Menus {
private Image star;
private Image circle;
private Image square;
private Image triangle;
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Menus");
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
if (circle != null) circle.dispose();
if (star != null) star.dispose();
if (square != null) square.dispose();
if (triangle != null) triangle.dispose();
display.dispose();
}
/**
* Creates the main window"s contents
*
* @param shell the main window
*/
private void createContents(Shell shell) {
shell.setLayout(new FillLayout());
createBarMenu(shell);
createPopUpMenu(shell);
createNoRadioGroupPopUpMenu(shell);
}
/**
* Creates the bar menu for the main window
*
* @param shell the main window
*/
private void createBarMenu(Shell shell) {
// Create the bar menu
Menu menu = new Menu(shell, SWT.BAR);
// Create all the items in the bar menu
MenuItem fileItem = new MenuItem(menu, SWT.CASCADE);
fileItem.setText("File");
MenuItem editItem = new MenuItem(menu, SWT.CASCADE);
editItem.setText("Edit");
MenuItem formatItem = new MenuItem(menu, SWT.CASCADE);
formatItem.setText("Format");
MenuItem viewItem = new MenuItem(menu, SWT.CASCADE);
viewItem.setText("View");
MenuItem helpItem = new MenuItem(menu, SWT.CASCADE);
helpItem.setText("Help");
// Create the File item"s dropdown menu
Menu fileMenu = new Menu(menu);
fileItem.setMenu(fileMenu);
// Create all the items in the File dropdown menu
MenuItem newItem = new MenuItem(fileMenu, SWT.NONE);
newItem.setText("New");
MenuItem openItem = new MenuItem(fileMenu, SWT.NONE);
openItem.setText("Open...");
MenuItem saveItem = new MenuItem(fileMenu, SWT.NONE);
saveItem.setText("Save");
MenuItem saveAsItem = new MenuItem(fileMenu, SWT.NONE);
saveAsItem.setText("Save As...");
new MenuItem(fileMenu, SWT.SEPARATOR);
MenuItem pageSetupItem = new MenuItem(fileMenu, SWT.NONE);
pageSetupItem.setText("Page Setup...");
MenuItem printItem = new MenuItem(fileMenu, SWT.NONE);
printItem.setText("Print...");
new MenuItem(fileMenu, SWT.SEPARATOR);
MenuItem exitItem = new MenuItem(fileMenu, SWT.NONE);
exitItem.setText("Exit");
// Set the bar menu as the menu in the shell
shell.setMenuBar(menu);
}
/**
* Creates the left-half pop-up menu
*
* @param shell the main window
*/
private void createPopUpMenu(Shell shell) {
// Create a composite that the pop-up menu will be
// associated with
Label label = new Label(shell, SWT.BORDER);
label.setText("Pop-up Menu");
// Create the pop-up menu
Menu menu = new Menu(label);
// Create the images
star = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(
"jexp.gif"));
circle = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(
"jexp.gif"));
square = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(
"jexp.gif"));
triangle = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(
"jexp.gif"));
// Create all the items in the pop-up menu
MenuItem newItem = new MenuItem(menu, SWT.CASCADE);
newItem.setText("New");
newItem.setImage(star);
MenuItem refreshItem = new MenuItem(menu, SWT.NONE);
refreshItem.setText("Refresh");
refreshItem.setImage(circle);
MenuItem deleteItem = new MenuItem(menu, SWT.NONE);
deleteItem.setText("Delete");
new MenuItem(menu, SWT.SEPARATOR);
// Add a check menu item and select it
MenuItem checkItem = new MenuItem(menu, SWT.CHECK);
checkItem.setText("Check");
checkItem.setSelection(true);
checkItem.setImage(square);
// Add a push menu item
MenuItem pushItem = new MenuItem(menu, SWT.PUSH);
pushItem.setText("Push");
new MenuItem(menu, SWT.SEPARATOR);
// Create some radio items
MenuItem item1 = new MenuItem(menu, SWT.RADIO);
item1.setText("Radio One");
item1.setImage(triangle);
MenuItem item2 = new MenuItem(menu, SWT.RADIO);
item2.setText("Radio Two");
MenuItem item3 = new MenuItem(menu, SWT.RADIO);
item3.setText("Radio Three");
// Create a new radio group
new MenuItem(menu, SWT.SEPARATOR);
// Create some radio items
MenuItem itema = new MenuItem(menu, SWT.RADIO);
itema.setText("Radio A");
MenuItem itemb = new MenuItem(menu, SWT.RADIO);
itemb.setText("Radio B");
MenuItem itemc = new MenuItem(menu, SWT.RADIO);
itemc.setText("Radio C");
// Create the New item"s dropdown menu
Menu newMenu = new Menu(menu);
newItem.setMenu(newMenu);
// Create the items in the New dropdown menu
MenuItem shortcutItem = new MenuItem(newMenu, SWT.NONE);
shortcutItem.setText("Shortcut");
MenuItem iconItem = new MenuItem(newMenu, SWT.NONE);
iconItem.setText("Icon");
// Set the pop-up menu as the pop-up for the label
label.setMenu(menu);
}
/**
* Creates the No Radio Group pop-up menu
*
* @param shell the main window
*/
private void createNoRadioGroupPopUpMenu(Shell shell) {
// Create a composite that the pop-up menu will be
// associated with
Label label = new Label(shell, SWT.BORDER);
label.setText("No Radio Group Menu");
// Create the pop-up menu with the No Radio Group style
Menu menu = new Menu(shell, SWT.POP_UP | SWT.NO_RADIO_GROUP);
label.setMenu(menu);
// Create all the items in the pop-up menu
MenuItem item1 = new MenuItem(menu, SWT.RADIO);
item1.setText("Radio One");
MenuItem item2 = new MenuItem(menu, SWT.RADIO);
item2.setText("Radio Two");
MenuItem item3 = new MenuItem(menu, SWT.RADIO);
item3.setText("Radio Three");
// Set the pop-up menu as the pop-up for the label
label.setMenu(menu);
}
/**
* The application entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new Menus().run();
}
}
/*
* Menu example snippet: enable menu items dynamically (when menu shown)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
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;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class Snippet73 {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
final Tree tree = new Tree (shell, SWT.BORDER | SWT.MULTI);
final Menu menu = new Menu (shell, SWT.POP_UP);
tree.setMenu (menu);
for (int i=0; i<12; i++) {
TreeItem treeItem = new TreeItem (tree, SWT.NONE);
treeItem.setText ("Item " + i);
MenuItem menuItem = new MenuItem (menu, SWT.PUSH);
menuItem.setText (treeItem.getText ());
}
menu.addListener (SWT.Show, new Listener () {
public void handleEvent (Event event) {
MenuItem [] menuItems = menu.getItems ();
TreeItem [] treeItems = tree.getSelection ();
for (int i=0; i<menuItems.length; i++) {
String text = menuItems [i].getText ();
int index = 0;
while (index<treeItems.length) {
if (treeItems [index].getText ().equals (text)) break;
index++;
}
menuItems [i].setEnabled (index != treeItems.length);
}
}
});
tree.setSize (200, 200);
shell.setSize (300, 300);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
/*
* Menu example snippet: fill a menu dynamically (when menu shown)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
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;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class Snippet97 {
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
final Tree tree = new Tree(shell, SWT.BORDER | SWT.MULTI);
final Menu menu = new Menu(shell, SWT.POP_UP);
tree.setMenu(menu);
for (int i = 0; i < 12; i++) {
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText("Item " + i);
}
menu.addListener(SWT.Show, new Listener() {
public void handleEvent(Event event) {
MenuItem[] menuItems = menu.getItems();
for (int i = 0; i < menuItems.length; i++) {
menuItems[i].dispose();
}
TreeItem[] treeItems = tree.getSelection();
for (int i = 0; i < treeItems.length; i++) {
MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText(treeItems[i].getText());
}
}
});
tree.setSize(200, 200);
shell.setSize(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Menu Examples
/*******************************************************************************
* All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
*
* Created on Feb 24, 2004 2:23:13 PM by JACK $Id$
*
******************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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 MenuExamples {
Display display = new Display();
Shell shell = new Shell(display);
public MenuExamples() {
init();
Menu menuBar = new Menu(shell, SWT.BAR);
MenuItem itemHello = new MenuItem(menuBar, SWT.PUSH);
itemHello.setText("&Hello");
itemHello.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("HELLO");
}
});
MenuItem itemCascade = new MenuItem(menuBar, SWT.CASCADE);
itemCascade.setText("&CASCADE item");
Menu menu = new Menu(itemCascade);
MenuItem itemPush = new MenuItem(menu, SWT.PUSH);
itemPush.setText("&PUSH item\tCtrl+P");
itemPush.setAccelerator(SWT.CTRL + "P");
Image icon = new Image(shell.getDisplay(), "jexp.gif");
itemPush.setImage(icon);
itemPush.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("item selected: PUSH item");
}
});
final MenuItem itemCheck = new MenuItem(menu, SWT.CHECK);
itemCheck.setText("CHEC&K item\tCtrl+K");
itemCheck.setAccelerator(SWT.CTRL + "K");
itemCheck.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("item selected: CHECK item");
System.out.println("Selection: " + itemCheck.getSelection());
}
});
new MenuItem(menu, SWT.SEPARATOR);
final MenuItem itemRadio = new MenuItem(menu, SWT.RADIO);
itemRadio.setText("&RADIO item\tCtrl+R");
itemRadio.setAccelerator(SWT.CTRL + "R");
itemRadio.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
System.out.println("item selected: RADIO item");
System.out.println("Selection: " + itemRadio.getSelection());
}
});
itemCascade.setMenu(menu);
//shell.setMenu(menuBar);
shell.setMenuBar(menuBar);
menuBar.setDefaultItem(itemCascade);
shell.setSize(300, 120);
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
}
public static void main(String[] args) {
new MenuExamples();
}
}
Menu Shell
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
public class MenuShell {
Display d;
Shell s;
MenuShell() {
d = new Display();
s = new Shell(d);
s.setSize(500, 500);
s.setImage(new Image(d, "jexp.gif"));
s.setText("A Shell Menu Example");
Menu m = new Menu(s, SWT.BAR);
s.setMenuBar(m);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new MenuShell();
}
}
Menu Shell 2
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 MenuShell2 {
Display d;
Shell s;
MenuShell2() {
d = new Display();
s = new Shell(d);
s.setSize(500, 500);
s.setText("A Shell Menu Example");
Menu m = new Menu(s, SWT.BAR);
MenuItem file = new MenuItem(m, SWT.CASCADE);
file.setText("File");
Menu filemenu = new Menu(s, SWT.DROP_DOWN);
file.setMenu(filemenu);
MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
openItem.setText("Open");
MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
exitItem.setText("Exit");
s.setMenuBar(m);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new MenuShell2();
}
}
Menu Shell 3
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class MenuShell3 {
Display d;
Shell s;
MenuShell3() {
d = new Display();
s = new Shell(d);
s.setSize(300, 300);
s.setText("A Shell Menu Example");
Menu m = new Menu(s, SWT.BAR);
// create a file menu and add an exit item
final MenuItem file = new MenuItem(m, SWT.CASCADE);
file.setText("File");
final Menu filemenu = new Menu(s, SWT.DROP_DOWN);
file.setMenu(filemenu);
final MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
openItem.setText("Open");
final MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
final MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
exitItem.setText("Exit");
// create an edit menu and add cut copy and paste items
final MenuItem edit = new MenuItem(m, SWT.CASCADE);
edit.setText("Edit");
final Menu editmenu = new Menu(s, SWT.DROP_DOWN);
edit.setMenu(editmenu);
final MenuItem cutItem = new MenuItem(editmenu, SWT.PUSH);
cutItem.setText("Cut");
final MenuItem copyItem = new MenuItem(editmenu, SWT.PUSH);
copyItem.setText("Copy");
final MenuItem pasteItem = new MenuItem(editmenu, SWT.PUSH);
pasteItem.setText("Paste");
//create a Window menu and add Child item
final MenuItem window = new MenuItem(m, SWT.CASCADE);
window.setText("Window");
final Menu windowmenu = new Menu(s, SWT.DROP_DOWN);
window.setMenu(windowmenu);
final MenuItem maxItem = new MenuItem(windowmenu, SWT.PUSH);
maxItem.setText("Maximize");
final MenuItem minItem = new MenuItem(windowmenu, SWT.PUSH);
minItem.setText("Minimize");
// create a Help menu and add an about item
final MenuItem help = new MenuItem(m, SWT.CASCADE);
help.setText("Help");
final Menu helpmenu = new Menu(s, SWT.DROP_DOWN);
help.setMenu(helpmenu);
final MenuItem aboutItem = new MenuItem(helpmenu, SWT.PUSH);
aboutItem.setText("About");
s.setMenuBar(m);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new MenuShell3();
}
}
Menu Shell 4
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class MenuShell4 {
Display d;
Shell s;
MenuShell4() {
d = new Display();
s = new Shell(d);
s.setSize(300, 300);
s.setText("A Shell Menu Example");
Menu m = new Menu(s, SWT.BAR);
// create a file menu and add an exit item
final MenuItem file = new MenuItem(m, SWT.CASCADE);
file.setText("File");
final Menu filemenu = new Menu(s, SWT.DROP_DOWN);
file.setMenu(filemenu);
final MenuItem openItem = new MenuItem(filemenu, SWT.CASCADE);
openItem.setText("Open");
final Menu submenu = new Menu(s, SWT.DROP_DOWN);
openItem.setMenu(submenu);
final MenuItem childItem = new MenuItem(submenu, SWT.PUSH);
childItem.setText("Child");
final MenuItem dialogItem = new MenuItem(submenu, SWT.PUSH);
dialogItem.setText("Dialog");
final MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
final MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
exitItem.setText("Exit");
// create an edit menu and add cut copy and paste items
final MenuItem edit = new MenuItem(m, SWT.CASCADE);
edit.setText("Edit");
final Menu editmenu = new Menu(s, SWT.DROP_DOWN);
edit.setMenu(editmenu);
final MenuItem cutItem = new MenuItem(editmenu, SWT.PUSH);
cutItem.setText("Cut");
final MenuItem copyItem = new MenuItem(editmenu, SWT.PUSH);
copyItem.setText("Copy");
final MenuItem pasteItem = new MenuItem(editmenu, SWT.PUSH);
pasteItem.setText("Paste");
//create a Window menu and add Child item
final MenuItem window = new MenuItem(m, SWT.CASCADE);
window.setText("Window");
final Menu windowmenu = new Menu(s, SWT.DROP_DOWN);
window.setMenu(windowmenu);
final MenuItem maxItem = new MenuItem(windowmenu, SWT.PUSH);
maxItem.setText("Maximize");
final MenuItem minItem = new MenuItem(windowmenu, SWT.PUSH);
minItem.setText("Minimize");
// create a Help menu and add an about item
final MenuItem help = new MenuItem(m, SWT.CASCADE);
help.setText("Help");
final Menu helpmenu = new Menu(s, SWT.DROP_DOWN);
help.setMenu(helpmenu);
final MenuItem aboutItem = new MenuItem(helpmenu, SWT.PUSH);
aboutItem.setText("About");
s.setMenuBar(m);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new MenuShell4();
}
}
Menu Shell 5
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Dialog;
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 MenuShell5 {
Display d;
Shell s;
MenuShell5() {
d = new Display();
s = new Shell(d);
s.setSize(500, 500);
s.setText("A Shell Menu Example");
Menu m = new Menu(s, SWT.BAR);
// create a file menu and add an exit item
final MenuItem file = new MenuItem(m, SWT.CASCADE);
file.setText("File");
final Menu filemenu = new Menu(s, SWT.DROP_DOWN);
file.setMenu(filemenu);
// create an open menu and to sub-menu items
final MenuItem openItem = new MenuItem(filemenu, SWT.CASCADE);
openItem.setText("Open");
final Menu submenu = new Menu(s, SWT.DROP_DOWN);
openItem.setMenu(submenu);
final MenuItem childItem = new MenuItem(submenu, SWT.PUSH);
childItem.setText("Child");
final MenuItem dialogItem = new MenuItem(submenu, SWT.PUSH);
dialogItem.setText("Dialog");
//add a separator
final MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
// create an exit menu item
final MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
exitItem.setText("Exit");
// create an edit menu and add cut copy and paste items
final MenuItem edit = new MenuItem(m, SWT.CASCADE);
edit.setText("Edit");
final Menu editmenu = new Menu(s, SWT.DROP_DOWN);
edit.setMenu(editmenu);
final MenuItem cutItem = new MenuItem(editmenu, SWT.PUSH);
cutItem.setText("Cut");
final MenuItem copyItem = new MenuItem(editmenu, SWT.PUSH);
copyItem.setText("Copy");
final MenuItem pasteItem = new MenuItem(editmenu, SWT.PUSH);
pasteItem.setText("Paste");
//create a Window menu and add Child item
final MenuItem window = new MenuItem(m, SWT.CASCADE);
window.setText("Window");
final Menu windowmenu = new Menu(s, SWT.DROP_DOWN);
window.setMenu(windowmenu);
final MenuItem maxItem = new MenuItem(windowmenu, SWT.PUSH);
maxItem.setText("Maximize");
final MenuItem minItem = new MenuItem(windowmenu, SWT.PUSH);
minItem.setText("Minimize");
// create a Help menu and add an about item
final MenuItem help = new MenuItem(m, SWT.CASCADE);
help.setText("Help");
final Menu helpmenu = new Menu(s, SWT.DROP_DOWN);
help.setMenu(helpmenu);
final MenuItem aboutItem = new MenuItem(helpmenu, SWT.PUSH);
aboutItem.setText("About");
childItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Shell parent = (Shell) maxItem.getParent().getParent();
ChildShell cs = new ChildShell(parent);
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
dialogItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Shell parent = (Shell) maxItem.getParent().getParent();
DialogExample de = new DialogExample(parent);
de.open();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
exitItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.exit(0);
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
cutItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Cut");
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
copyItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Copy");
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
pasteItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Paste");
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
maxItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Shell parent = (Shell) maxItem.getParent().getParent();
parent.setMaximized(true);
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
minItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Shell parent = (Shell) minItem.getParent().getParent();
parent.setMaximized(false);
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
aboutItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Shell parent = (Shell) minItem.getParent().getParent();
parent.setMaximized(false);
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
s.setMenuBar(m);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new MenuShell5();
}
}
class DialogExample extends Dialog {
DialogExample(Shell parent) {
super(parent);
}
public String open() {
Shell parent = getParent();
Shell dialog = new Shell(parent, SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
dialog.setSize(100, 100);
dialog.setText("Java Source and Support");
dialog.open();
Display display = parent.getDisplay();
while (!dialog.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return "After Dialog";
}
public static void main(String[] argv) {
new DialogExample(new Shell());
}
}
class ChildShell {
ChildShell(Shell parent) {
Shell child = new Shell(parent);
child.setSize(200, 200);
child.open();
}
}
Menu Shell 6
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 MenuShell6 {
Display d;
Shell s;
MenuShell6() {
d = new Display();
s = new Shell(d);
s.setSize(500, 500);
s.setText("A Shell Menu Example");
final Menu m = new Menu(s, SWT.BAR);
// create a file menu and add an exit item
final MenuItem file = new MenuItem(m, SWT.CASCADE);
file.setText("&File");
final Menu filemenu = new Menu(s, SWT.DROP_DOWN);
file.setMenu(filemenu);
final MenuItem openItem = new MenuItem(filemenu, SWT.CASCADE);
openItem.setText("&Open");
final Menu submenu = new Menu(s, SWT.DROP_DOWN);
openItem.setMenu(submenu);
final MenuItem childItem = new MenuItem(submenu, SWT.PUSH);
childItem.setText("&Child\tCTRL+C");
childItem.setAccelerator(SWT.CTRL + "C");
final MenuItem dialogItem = new MenuItem(submenu, SWT.PUSH);
dialogItem.setText("&Dialog\tCTRL+D");
dialogItem.setAccelerator(SWT.CTRL + "D");
final MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
final MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
exitItem.setText("E&xit");
// create an edit menu and add cut copy and paste items
final MenuItem edit = new MenuItem(m, SWT.CASCADE);
edit.setText("&Edit");
final Menu editmenu = new Menu(s, SWT.DROP_DOWN);
edit.setMenu(editmenu);
final MenuItem cutItem = new MenuItem(editmenu, SWT.PUSH);
cutItem.setText("&Cut");
final MenuItem copyItem = new MenuItem(editmenu, SWT.PUSH);
copyItem.setText("Co&py");
final MenuItem pasteItem = new MenuItem(editmenu, SWT.PUSH);
pasteItem.setText("&Paste");
//create an options menu and add menu items
final MenuItem options = new MenuItem(m, SWT.CASCADE);
options.setText("&Options");
final Menu optionsmenu = new Menu(s, SWT.DROP_DOWN);
options.setMenu(optionsmenu);
final MenuItem checkItem = new MenuItem(optionsmenu, SWT.CHECK);
checkItem.setText("&Checked Option");
final MenuItem optionsseparator = new MenuItem(optionsmenu,
SWT.SEPARATOR);
final MenuItem radioItem1 = new MenuItem(optionsmenu, SWT.RADIO);
radioItem1.setText("Radio &One");
final MenuItem radioItem2 = new MenuItem(optionsmenu, SWT.RADIO);
radioItem2.setText("Radio &Two");
//create a Window menu and add Child item
final MenuItem window = new MenuItem(m, SWT.CASCADE);
window.setText("&Window");
final Menu windowmenu = new Menu(s, SWT.DROP_DOWN);
window.setMenu(windowmenu);
final MenuItem maxItem = new MenuItem(windowmenu, SWT.PUSH);
maxItem.setText("Ma&ximize");
final MenuItem minItem = new MenuItem(windowmenu, SWT.PUSH);
minItem.setText("Mi&nimize");
// create a Help menu and add an about item
final MenuItem help = new MenuItem(m, SWT.CASCADE);
help.setText("&Help");
final Menu helpmenu = new Menu(s, SWT.DROP_DOWN);
help.setMenu(helpmenu);
final MenuItem aboutItem = new MenuItem(helpmenu, SWT.PUSH);
aboutItem.setText("&About");
// add action listeners for the menu items
// this code is the same as seen previously so it is
// omitted here
s.setMenuBar(m);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new MenuShell6();
}
}
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 5, 2004 2:50:44 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
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 SharedMenu {
Display display = new Display();
Shell shell = new Shell(display);
public SharedMenu() {
menu();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void menu() {
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);
}
/*
private void complex() {
shell.setLayout(new GridLayout(2, true));
Label label = new Label(shell, SWT.NULL);
label.setText("Label");
Composite composite = new Composite(shell, SWT.NULL);
composite.setLayout(new GridLayout());
Button button1 = new Button(composite, SWT.PUSH);
button1.setText("Button 1");
Button button2 = new Button(composite, SWT.PUSH);
button2.setText("Button 2");
}
private void addButton() {
shell.setLayout(new GridLayout());
Label label = new Label(shell, SWT.NULL);
final Color color = new Color(display, 255, 0, 0);
label.setForeground(color);
label.setForeground(display.getSystemColor(SWT.COLOR_RED));
label.setText("Testing label");
label.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
color.dispose();
}
});
}
private void draw() {
GC gc = new GC(shell);
gc.setLineWidth(3);
//Color color = new Color(display, 255, 0, 0);
//gc.setForeground(color);
gc.drawRectangle(10, 10, 200, 100);
gc.dispose();
}
*/
public static void main(String[] args) {
new SharedMenu();
}
}
/*
* Menu example snippet: show a popup menu (wait for it to close)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
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 Snippet131 {
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();
}
}
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();
}
}
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();
}
}