Java Reference
In-Depth Information
To prevent clutter, we omitted the
java.util.List
interface from the diagram in Figure
7-1
. The
java.util.List
interface is the other super interface of
ObservableList
. The following two methods on the
ObservableList
interface
allow you to register and unregister
ListChangeListener
s:
•
addListener(ListChangeListener<? super E> listener)
removeListener(ListChangeListener<? super E> listener)
The following additional methods on
ObservableList
make working with the interface easier:
•
•
addAll(E... elements)
•
setAll(E... elements)
•
setAll(Collection<? extends E> col)
•
removeAll(E... elements)
•
retainAll(E... elements)
•
remove(int from, int to)
•
filtered(Predicate<E>)
•
sorted(Comparator<E>)
sorted()
The
filtered()
and the two
sorted()
methods return a
FilteredList
or a
SortedList
that wraps the
ObservableList
. When the original
ObservableList
is mutated, the wrapper
FilteredList
and
SortedList
reflect
the changes.
The
ListChangeListener
interface has only one method:
onChange(ListChangeListener.Change<? extends
E> change).
This method is called back when the content of the
ObservableList
is manipulated. Notice that this
method's parameter type is the nested class
Change
that is declared in the
ListChangeListener
interface. We show
you how to use the
ListChangeListener.Change
class in the next subsection. For now, we look at a simple example in
Listing 7-1 illustrating the firing of invalidation and list change events when an
ObservableList
is manipulated.
•
Listing 7-1.
ObservableListExample.java
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import static javafx.collections.ListChangeListener.Change;
public class ObservableListExample {
public static void main(String[] args) {
ObservableList<String> strings = FXCollections.observableArrayList();
strings.addListener((Observable observable) -> {
System.out.println("\tlist invalidated");
});
Search WWH ::
Custom Search