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
-1if 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[] ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access