Adding Widgets Other Than Buttons to the CoolBar

It is possible to add other types of widgets to the CoolBar in the same manner as was done in Example 15-1 with Button. However, it only makes sense to consider a couple widget types for inclusion on a CoolBar. List, for example, is not a good candidate, since the room required for the List would exceed the amount of space available (top to bottom) on the CoolBar.

Combo is a good candidate for inclusion on a CoolBar when you must enable your user to select an item from a list.

Note

Not only that, but it would be ugly.

How do I do that?

You add a Combo to the CoolBar using the same steps that are used to add a Button. Use this code to add a Combo to CoolbarShellExample from Example 15-1:

final CoolItem fontCoolItem = new CoolItem(bar, SWT.PUSH);
final Combo fontCombo = new Combo(bar, SWT.READ_ONLY | SWT.BORDER);
String[] items = {"Arial", "Courier", "Times New Roman"};
fontCombo.setItems(items);
fontCombo.pack( );
size = fontCombo.getSize( );
fontCoolItem.setControl(fontCombo);
fontCoolItem.setSize(fontCoolItem.computeSize(size.x, size.y));
fontCoolItem.setMinimumSize(size);

Doing so yields the result shown in Figure 15-2.

Using a Combo on a CoolBar

Figure 15-2. Using a Combo on a CoolBar

A selectionListener can be associated with the Combo to take appropriate action when the user selects an item.

fontCombo.addSelectionListener(new SelectionAdapter( ) { public void widgetSelected(SelectionEvent ...

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.