View RSS Feed

My Java Tips

SWT Menu

Rate this Entry
by , 11-10-2011 at 06:10 PM (1144 Views)
Menu are user interface objects that contain menu items. The Menu support styles BAR, DROP_DOWN, POP_UP, NO_RADIO_GROUP, LEFT_TO_RIGHT, RIGHT_TO_LEFT. Please note that only one of BAR, DROP_DOWN and POP_UP may be specified. Only one of LEFT_TO_RIGHT or RIGHT_TO_LEFT may be specified.


The following are the important constructors of Menu class.

Menu(Control parent)
Constructs a new instance of this class given its parent, and sets the style for the instance so that the instance will be a popup menu on the given parent's shell.

Menu(Decorations parent, int style)
Constructs a new instance of this class given its parent (which must be a Decorations) and a style value describing its behavior and appearance.

Menu(Menu parentMenu)
Constructs a new instance of this class given its parent (which must be a Menu) and sets the style for the instance so that the instance will be a drop-down menu on the given parent's parent.

Menu(MenuItem parentItem)
Constructs a new instance of this class given its parent (which must be a MenuItem) and sets the style for the instance so that the instance will be a drop-down menu on the given parent's parent menu.

The following are the important methods of Menu class.

void addMenuListener(MenuListener listener)
Adds the listener to the collection of listeners who will be notified when menus are hidden or shown, by sending it one of the messages defined in the MenuListener interface.

MenuItem getDefaultItem()
Returns the default menu item or null if none has been previously set.

boolean getEnabled()
Returns true if the receiver is enabled, and false otherwise.

MenuItem getItem(int index)
Returns the item at the given, zero-relative index in the receiver.

int getItemCount()
Returns the number of items contained in the receiver.

MenuItem[] getItems()
Returns a (possibly empty) array of MenuItems which are the items in the receiver.

Menu getParentMenu()
Returns the receiver's parent item, which must be a Menu or null when the receiver is a root.

Shell getShell()
Returns the receiver's shell.

boolean getVisible()
Returns true if the receiver is visible, and false otherwise.

int indexOf(MenuItem item)
Searches the receiver's list starting at the first item (index 0) until an item is found that is equal to the argument, and returns the index of that item.

boolean isEnabled()
Returns true if the receiver is enabled and all of the receiver's ancestors are enabled, and false otherwise.

boolean isVisible()
Returns true if the receiver is visible and all of the receiver's ancestors are visible and false otherwise.

void setDefaultItem(MenuItem item)
Sets the default menu item to the argument or removes the default emphasis when the argument is null.

void setEnabled(boolean enabled)
Enables the receiver if the argument is true, and disables it otherwise.

void setLocation(int x, int y)
Sets the location of the receiver, which must be a popup, to the point specified by the arguments which are relative to the display.

void setLocation(Point location)
Sets the location of the receiver, which must be a popup, to the point specified by the argument which is relative to the display.

void setVisible(boolean visible)
Marks the receiver as visible if the argument is true, and marks it invisible otherwise.

Example

The example below creates a simple bar and a pull down menu. It consists File Menu with MenuItem Select All. When you select it it shows Select All on console.

Java Code:
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

public class SWTMenu {

	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 &AlltCtrl+A");
		item.setAccelerator(SWT.MOD1 + 'A');
		shell.setSize(200, 200);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

}

Submit "SWT Menu" to Facebook Submit "SWT Menu" to Digg Submit "SWT Menu" to del.icio.us Submit "SWT Menu" to StumbleUpon Submit "SWT Menu" to Google

Tags: menu, menu items, swt Add / Edit Tags
Categories
SWT / JFace

Comments