Custom widgets

You may want to extend SWT by implementing your own custom widget. SWT itself provides a package, org.eclipse.swt.custom, which contains custom controls that are not in the core set of SWT controls but are needed to implement the platform workbench.

Control
Purpose
CBanner CBanner is used in the workbench to layout the toolbar area and perspective switching toolbar.
CCombo Similar to Combo, but is vertically resizable allowing it to fit inside table cells.
CLabel Similar to Label, but supports shortening of text with an ellipsis. Also supports a gradient effect for the background color as seen in the active workbench view. Does not support wrapping.
CTabFolder Similar to TabFolder, but supports additional configuration of the visual appearance of tabs (top or bottom) and borders.
CTabItem Selectable user interface object corresponding to a tab for a page in a CTabFolder.
SashForm Composite control that lays out its children in a row or column arrangement and uses a Sash to separate them so that the user can resize them.
ScrolledComposite Composite control that scrolls its contents and optionally stretches its contents to fill the available space.
StyledText Editable control that allows the user to type text. Ranges of text inside the control can have distinct colors and font styles.
ViewForm ViewForm is used in the workbench to position and size a view's label/toolbar/menu local bar.

Implementing a custom widget

Once you've determined that you need a custom widget and have decided which platforms must be supported, you can consider several implementation techniques for your widget. These techniques can be mixed and matched depending on what is available in the underlying OS platform.

Native implementation

If your application requires a native widget that is not provided by SWT, you will need to implement it natively. This may be a platform widget, a third party widget, or any other widget in a platform shared library. A complete example of a native custom widget implementation can be found in Creating Your Own Widgets using SWT.

Combining existing widgets

Widgets can be combined to form widgets that are more sophisticated. For example, a Combo can be implemented using a text entry widget along with a button and a drop-down list. To implement a combined widget, you create a subclass of Composite and manage the children internally.

A simple example can be found in CCombo.

Custom drawn implementation

In some cases, you don't have any native code or existing widgets that help you in the implementation of your new widget. This means you must draw the widget yourself in the handler for the Paint event. Although this technique can become quite complicated, it has the advantage of producing a completely portable implementation.

Custom drawn controls are implemented by subclassing the Canvas or Composite. Subclass Canvas if your widget will not contain any child controls.

The internal implementation of a custom drawn widget usually involves these major tasks:

Many of the widgets implemented in the org.eclipse.swt.custom use this approach. A simple example can be found in CLabel.

Further information on custom widgets can be found in Creating your own widgets using SWT.