DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 9.8 Creating Text Menu Items

9.8.1 Problem

You want to add menu items that display text captions to a menu and handle selection events for those items.

9.8.2 Solution

Use the MenuItem class and the SelectionAdapter class. Create a MenuItem object, use the addSelectionListener method to add a listener to it, and use the setText method to set the text caption in the menu item.

9.8.3 Discussion

To implement a File Save item to the menu example we started in the previous recipe, create a new MenuItem object with the caption Save, pass the fileMenu object to its constructor, and give this item the style SWT.PUSH:

public MenuClass( )
     .
     .
     .
    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");
     .
     .
     .

To make this menu item active, connect it to a selection listener class we'll name MenuListener:

fileSaveItem.addSelectionListener(new MenuItemListener( ));

The MenuListener class will extend the SelectionAdapter class. As mentioned in Chapter 8, all SWT listener interfaces have adapter classes with stub implementations of all the interface's methods; if you extend an adapter class, you have to implement only the methods you want to override. In this case, we'll display the text of the selected item by retrieving the menu item widget that caused the event; note that if the selected item was File Exit, we exit:

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( ));
    }
}

That completes the File Save menu item; we'll add File Exit and Edit Copy items to this example too, as shown in Example 9-3.

Example 9-3. SWT menus
package org.cookbook.ch09;

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;

public class MenuClass
{
    Display display;
    Shell shell;
    Menu menuBar, fileMenu, editMenu;
    MenuItem fileMenuHeader, editMenuHeader;
    MenuItem fileExitItem, fileSaveItem, 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");

        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( ));

        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( ));
        }
    }

    public static void main(String[] args)
    {
        MenuClass menuExample = new MenuClass( );
    }
}

You can see the results in Figure 9-6, where we're selecting File Save.

Figure 9-6. Opening the File menu
figs/ecb_0906.gif


If you do select File Save, you'll see the selected item displayed in the application's text widget, as shown in Figure 9-7.

Figure 9-7. Selecting a menu item
figs/ecb_0907.gif


9.8.4 See Also

Recipe 9.7 on creating a menu system; Recipe 9.9 on creating image menu items; Recipe 9.10 on creating radio menu items; Recipe 9.11 on creating menu item accelerators and mnemonics; Recipe 9.12 on enabling and disabling menu items; Chapter 8 in Eclipse (O'Reilly).

    Previous Section  < Day Day Up >  Next Section