Java Reference
In-Depth Information
boolean wasAdded()
•
boolean wasRemoved()
•
boolean wasReplaced()
•
boolean wasPermutated()
•
int getFrom()
•
int getTo()
•
int getAddedSize()
•
List<E> getAddedSublist()
•
int getRemovedSize()
•
List<E> getRemoved()
•
int getPermutation(int i)
•
ObservableList<E> getList()
The
next()
and
reset()
methods control a cursor that iterates through all the discrete changes in the event
object. On entry to the
onChange()
method of
ListChangeListener
, the cursor is positioned before the first discrete
change. You must call the
next()
method to move the cursor to the first discrete change. Succeeding calls to the
next()
method will move the cursor to the remaining discrete changes. If the next discrete change is reached, the
return value will be
true
. If the cursor is already on the last discrete change, the return value will be
false
. Once
the cursor is positioned on a valid discrete change, the methods
wasAdded()
,
wasRemoved()
,
wasReplaced()
, and
wasPermutated()
can be called to determine the kind of change the discrete change represents.
•
■
the
wasAdded()
,
wasRemoved()
,
wasReplaced()
, and
wasPermutated()
methods are not orthogonal.
a discrete change is a replacement only if it is both an addition and a removal. the proper order for testing the kind of
a discrete change, therefore, is to first determine whether it is a permutation or a replacement and then to determine
whether it is an addition or a removal.
Caution
Once you have determined the kind of discrete change, you can call the other methods to get more information
about it. For addition, the
getFrom()
method returns the index in the observable list where new elements were
added; the
getTo()
method returns the index of the element that is one past the end of the added elements; the
getAddedSize()
method returns the number of elements that were added; and the
getAddedSublist()
method
returns a
List<E>
that contains the added elements. For removal, the
getFrom()
and
getTo()
methods both return
the index in the observable list where elements were removed; the
getRemovedSize()
method returns the number of
elements that were removed; and the
getRemoved()
method returns a
List<E>
that contains the removed elements.
For replacement, both the methods that are relevant for addition and the methods that are relevant for removal
should be examined, because a replacement can be seen as a removal followed by an addition at the same index.
For permutation, the
getPermutation(int i)
method returns the index of an element in the observable list after the
permutation whose index in the observable list before the permutation was
i
. In all situations, the
getList()
method
always returns the underlying observable list.
In the example shown in Listing 7-2, we perform various list manipulations after attaching a
ListChangeListener
to an
ObservableList
. The implementation of
ListChangeListener
, called
MyListener
, includes a pretty printer for
the
ListChangeListener.Change
object, and prints out the list change event object when an event is fired.
Search WWH ::
Custom Search