8.8. Creating List Widgets

Problem

You need to display multiple items from which the user can select.

Solution

Use a list widget, which can display lists of selectable items. Here’s a selection of the most popular list widget methods:

void add(String string)

Adds the argument to the end of the list widget’s list

void add(String string, int index)

Adds the argument to the list widget’s list at the given zero-relative index

void addSelectionListener(SelectionListener listener)

Adds the listener to the collection of listeners who are notified when the list widget’s selection changes

String getItem(int index)

Returns the item at the given zero-relative index in the list widget

int getItemCount( )

Returns the number of items contained in the list widget

String[] getItems( )

Returns an array of Strings that are the items in the list widget

String[] getSelection( )

Returns an array of Strings that are currently selected in the list widget

int getSelectionCount( )

Returns the number of selected items contained in the list widget

int getSelectionIndex( )

Returns the zero-relative index of the item that is currently selected in the list widget, or -1 if no item is selected

int[] getSelectionIndices( )

Returns the zero-relative indices of the items that are currently selected in the list widget

void setItems(String[] items)

Sets the list widget’s items to be the given array of items

void setSelection(int index)

Selects the item at the given zero-relative index in the list widget

void setSelection(int[] ...

Get Eclipse Cookbook 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.