Java Reference
In-Depth Information
void
getSelectionMode()
void
setVisibleRowCount(
int
rowNo)
int
getSelectedIndex()
int
[] getSelectedIndices()
Object getSelectedValue()
Object[] getSelectedValues()
JList()
creates an empty list.
JList(String[] entries)
creates a list which contains the strings of array
en-
tries
. The first string is the first, i.e. top-most, list entry.
setListData(String[] entries)
sets the list entries to the strings in array
en-
tries
. The first string is the first, i.e. top-most, list entry.
setSelectionMode(int)
sets the way in which the user is allowed to select list
entries. One can choose between the following:
SINGLE_SELECTION
- only one
list entry can be selected, if a new one is selected the old selection is dropped;
SINGLE_INTERVAL_SELECTION
- one group of adjacent entries can be selected;
MULTIPLE_INTERVAL_SELECTION
- arbitrary combinations of entries can be
selected. The three constants are defined in class
ListSelectionModel
.
getSelectionMode()
determines which selection mode is currently used. The
returned integer value is one of those described for method
setSelectionMode
above.
setVisibleRowCount(int rowNo)
sets the number of rows visible of the list. This
adjusts the height of the graphical component such that
rowNo
shows how many
rows are displayed.
getSelectedIndex()
returns the index of the (first) selected entry or
−
1ifno
entry is selected.
getSelectedIndices()
returns the indices of the selected entries in an integer
array.
getSelectedValue()
returns the (first) selected entry (not the index but the
entry itself) or
null
if nothing is selected.
getSelectedValues()
returns the selected entries (not the indices but the entries
themselves), or
null
if nothing is selected.
16.2.1
Filling a list with data
To get text entries into a list one can pass an array of strings in the constructor. The
following program
ListDemo
creates a list with the names of the German provinces.
The visible length of the list is set to 8. To make all entries accessible we place
the list into the viewport of a scroll pane. The scroll pane is then embedded into
the frame. We do not set the selection mode, so the list has the default selection
mode which is
MULTIPLE_INTERVAL_SELECTION
. The
main
-method is placed in
the frame class so no driver class is needed.
Search WWH ::
Custom Search