9.7. Creating a Menu System
Problem
You want to create and add a menu system to your SWT application.
Solution
Create a menu system with Menu
and
MenuItem
objects. You create a new
Menu
object for the menu system itself, and new
MenuItem
objects for the individual menus and menu
items.
Discussion
You use Menu
and MenuItem
objects to create menu systems in SWT; to get a handle on this
process, we’re going to create a menu system here,
with File and Edit menus. When the user selects a menu item
we’ll display the item he has selected in a text
widget.
This example is MenuApp
at this
book’s site. You start by creating a standard menu
system with a Menu
object corresponding to the
menu bar. Here is a selection of popular Menu
methods:
-
void addMenuListener(MenuListener listener)
Adds the listener to the collection of listeners who will be notified when menus are hidden or shown
-
MenuItem getItem(int index)
Returns the item at the given index
-
int getItemCount( )
Returns the number of items contained in the menu
-
void setVisible(boolean visible)
Makes the menu visible if the argument is true, invisible otherwise
Here’s our menu bar, created with the
SWT.BAR
style:
public class MenuClass
{
Display display;
Shell shell;
Menu menuBar;
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);
.
.
.
After creating ...
Get Eclipse Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.