Toolbars

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:

  • The ToolBar styles are as follows:
    • SWT.WRAP: Wrap to fit the visible area
    • SWT.FLAT: Toolbar with a flat style
    • SWT.HORIZONTAL: Layout buttons horizontally
    • SWT.VERTICAL: Layout buttons vertically
    • SWT.RIGHT: Place the text in the right of an item
    • SWT.SHADOW_OUT: Separator above the toolbar
  • The ToolItem styles are as follows:
    • SWT.PUSH: Create a push button
    • SWT.CHECK: Create a check button
    • SWT.RADIO: Create a radio button
    • SWT.SEPARATOR: Create a separator
    • SWT.DROP_DOWN: Create a drop-down item
  • The ToolItem events are as follows:
    • SWT.Selection: Tool item was selected

In 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset