Working with Other Menu Item Styles

In addition to SWT.SEPARATOR, SWT.PUSH, and SWT.CASCADE, which you have exercised earlier, menu items can have one of two other styles: SWT.RADIO and SWT.CHECK.

The RADIO style assigns multiple menu items to a group of items and allows only one of those items to be selected at any given time. It is analogous to the RADIO style for buttons that you will examine in Chapter 5.

The CHECK style simply places a check icon next to a menu item when the user has selected that option and removes it when the user deselects that option (by clicking the menu while it is in its checked state).

Both of these styles are useful when you want to enable the user to set a system option from the menu and to retain a visual indication of what options have been previously selected.

How do I do that?

The following code creates an Options menu with three menu items (plus a separator):

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

Adding this code to the ShellMenu example class results ...

Get SWT: A Developer's Notebook 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.