A toolbar is a row of buttons that is shown between the menu and the working area. Despite being rather common, it is not always used by applications. Toolbars can have a text label or an image. Usually, images that provide a hint of the behavior of the buttons are used together with tooltips' help texts when the mouse hovers over the buttons.
The way of creating toolbars is rather similar to creating menus. The ToolBar
class is considered a container for individual buttons, which are represented by the ToolItem
objects. The following styles can be used for the ToolBar
and ToolItem
objects:
ToolBar
styles are as follows:SWT.WRAP
: Wrap to fit the visible areaSWT.FLAT
: Toolbar with a flat styleSWT.HORIZONTAL
: Layout buttons horizontallySWT.VERTICAL
: Layout buttons verticallySWT.RIGHT
: Place the text in the right of an itemSWT.SHADOW_OUT
: Separator above the toolbarToolItem
styles are as follows:SWT.PUSH
: Create a push buttonSWT.CHECK
: Create a check buttonSWT.RADIO
: Create a radio buttonSWT.SEPARATOR
: Create a separatorSWT.DROP_DOWN
: Create a drop-down itemToolItem
events are as follows:SWT.Selection
: Tool item was selectedIn the following code snippet, we show a very simple example regarding the creation of a toolbar along with tool items. It is also possible to use images instead of texts.
... Display display = new Display(); Shell shell = new Shell(display); ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL); ToolItem item = new ToolItem(toolBar, SWT.DROP_DOWN); item.setText("Drop Down Behavior (Combo)"); toolBar.pack(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); ...
We can see that a ToolBar
item is created, and afterwards a tool item with a drop-down behavior is inserted into it. In a real-world application, it would probably be necessary to add a listener to the ToolItems
objects, in order that the generated events could be handled.