Java Reference
In-Depth Information
!
Changes in the list model (additions or deletions of entries) are
automatically
displayed by the corresponding list. No call to
repaint
is necessary.
16.2.4
List listeners and list events
As specified above, our application has to react to a list entry being selected. The
appropriate listener for this purpose is a
ListSelectionListener
.Itisassigned
to a
JList
, say
myjlist
,by
myjlist.addListSelectionListener(listener)
Interface
ListSelectionListener
requires the implementation of only one
method:
public void
valueChanged(ListSelectionEvent evt)
This method is automatically called by the runtime system every time the selection
!
is changed. It is not executed if the selection is unchanged, for example if the user
clicks in an item that is already selected. The body of this method has to contain
the code that should be executed in response to the selection.
Method
valueChanged
receives an object of type
ListSelectionEvent
as an
argument. This is automatically generated by the runtime system and contains
further information on the selection event that has occurred. We shall use the
following methods of
ListSelectionEvent
:
int
getFirstIndex()
int
getLastIndex()
boolean
getIsAdjusting()
getFirstIndex()
returns the position of the first (top-most) entry, the selection
mode of which might have changed (from selected to non-selected or vice
versa). 'Might' refers to the fact that the section state might be the same as
before. For example if entries 5 to 8 were selected and the user now selects 5 to
10 then the first selected index remains unchanged. Which entries are selected
can be found by using method
getSelectedIndices
of class
JList
.
getLastIndex()
returns the position of the last (bottom-most) entry, the selection
mode of which might have changed. See also
getFirstIndex()
.
getIsAdjusting
- changing the selection in a list triggers a series of actions, each
!
of which generates a list selection event. For example previously selected en-
tries become unselected before new ones become selected. Usually, one would
prefer this series of events to be completed before the application reacts to the
change. For such a rapid series of events all but the last one return
true
as a
result of
getIsAdjusting
. Only the last one returns
false
as a result of
getI-
sAdjusting
. This can be used to wait for the end of the series of adjustments
before taking further action.
!
At the moment,
ListSelectionEvent
supports only the selection modes
SIN-
GLE_SELECTION
and
SINGLE_INTERVAL_SELECTION
.
Search WWH ::
Custom Search