Adding Scrollbars to a List

Scrollbars are necessary when the list is longer than the space available to view all the items. This is often the case, given that there is almost never enough room in a window for all interface elements. Scrollbars are added as a style attribute to the list at the time it is created.

How do I do that?

As with Text widgets, there are two scrollbar styles—SWT.V_SCROLL and SWT.H_SCROLL. For proper interface design, the scrollbar type you want to use most often is SWT.V_SCROLL:

Note

The scrollbar will appear only when there are more items than can be seen in the list.

final List l = new List(s, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);

You can specify SWT.H_SCROLL in all occasions, since it, too, will not appear unless needed.

And that’s a wrap on this discussion of the SWT List class, one of the major widgets provided for building your user interfaces. In the next chapter, you will learn how to use the Combo class, which is closely related to, and shares much functionality with, List.

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.