Using Composite Class Styles

Composite will accept several styles. The first is SWT.NO_BACKGROUND , which causes the Composite to be transparent. This is used to allow whatever is underneath the Composite, perhaps a background color or picture, to show through. SWT.NO_FOCUS can be used to prevent the Composite itself from gaining the input focus as the user tabs through controls on the window. Additionally, Composite can accept styles that apply to other widgets, such as SWT.BORDER , as illustrated by the previous example above.

As with all styles, you must experiment with different combinations until you derive the desired effect.

What about...

if you want to enable the end developer—the one who may be using your Composite subclass to develop applications—to specify a style? In TextPaneComposite, from Example 10-1, you see that the class makes a call to the constructor of the Composite class:

super(parent, SWT.BORDER);

This is necessary due to a rule of the Java compiler that requires a call to a specific constructor on the super-class whenever the super-class does not contain a default, no-argument, constructor. The Composite class is such a class—all of its constructors require a reference to the parent of the Composite being constructed.

In the case of TextPaneComposite , the style parameter is hardcoded into the call to the parent’s constructor. Although this serves the purpose of the example, it does make the resulting class a little inflexible. A much better approach is ...

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.