Chapter 7. SWT Lists

List widgets are used to present information to the user and allow selection of an item or items for further action (when processing is initiated by a command button, for example). List is similar in concept to the Combo, or drop-down, list, which is the subject of Chapter 8. The choice between using a List or a Combo is simple: use a List when it is either important for the user to have some or all of the items to choose from visible at all times, or when screen real estate is not an important design consideration.

A single SWT class is used to construct all lists—the List class—located in the org.eclipse.swt.widgets package.

Creating a List

A typical list is shown in Figure 7-1.

A simple list

Figure 7-1. A simple list

As with all widgets, the first step in using a list is to create the List object—an instance of the List class—then position the list within the container as desired. Once a List object has been created, it can be populated with items.

How do I do that?

To create a list you must create an instance of the List class, passing the constructor a reference to a shell or other container, and specifying the style of list you wish to create. The list in Figure 7-1 is created using the code in Example 7-1.

Example 7-1. Creating a list

import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.*; public class ListExample { Display d; Shell ...

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.