Java Reference
In-Depth Information
strings.addListener((Change<? extends String> change) -> {
System.out.println("\tstrings = " + change.getList());
});
System.out.println("Calling add(\"First\"): ");
strings.add("First");
System.out.println("Calling add(0, \"Zeroth\"): ");
strings.add(0, "Zeroth");
System.out.println("Calling addAll(\"Second\", \"Third\"): ");
strings.addAll("Second", "Third");
System.out.println("Calling set(1, \"New First\"): ");
strings.set(1, "New First");
final List<String> list = Arrays.asList("Second_1", "Second_2");
System.out.println("Calling addAll(3, list): ");
strings.addAll(3, list);
System.out.println("Calling remove(2, 4): ");
strings.remove(2, 4);
final Iterator<String> iterator = strings.iterator();
while (iterator.hasNext()) {
final String next = iterator.next();
if (next.contains("t")) {
System.out.println("Calling remove() on iterator: ");
iterator.remove();
}
}
System.out.println("Calling removeAll(\"Third\", \"Fourth\"): ");
strings.removeAll("Third", "Fourth");
}
}
Unlike the Java collections framework, where the public API contains both the interfaces, such as
List
,
Map
,
and
Set
, and concrete implementations of the interfaces that you can instantiate, such as
ArrayList
,
HashMap
, and
HashSet
, the JavaFX observable collections framework provides only the interfaces
ObservableList
,
ObservableMap
,
ObservableSet
, and
ObservableArray
, but not concrete implementation classes. To obtain an object of a JavaFX
observable collections and arrays interface, you use the utility class
FXCollections
. In Listing 7-1, we obtain an
ObservableList<String>
object by calling a factory method on
FXCollections
:
ObservableList<String> strings = FXCollections.observableArrayList();
We then hooked an
InvalidationListener
and a
ListChangeListener
to the observable list. Because both
listeners have a one-argument method and are added using
addListener()
, we have to specify the parameter type in the
lambda expressions. The invalidation listener simply prints out a message every time it's called. The list change listener
prints out the content of the observable list. The rest of the program simply manipulates the content of the observable list
in various ways: by calling methods on the
java.util.List
interface, by calling some of the new convenience methods
added to
ObservableList
, and by calling the
remove()
method on an
Iterator
obtained from the observable list.
Search WWH ::
Custom Search