As interfaces ObservableList, ObservableMap, ObservableSet e ObservableArray fornece os métodos addListener() e removeListener() para registrar e remover listeners para notificar mudanças para coleção ou matriz subjacente.
As interfaces correspondentes ListChangeListener, MapChangeListener e SetChangeListener cada um tem o método callback onChanged() que tem os parâmetros dentro da classe Change.
A interface ArrayChangeListener tem o método callback onChanged() com parâmetros explícitos.
O exemplo mostra um programa onde um ObservableList<String> é manipulado e o objeto correspondente Change é inquirido a anexar e implementar ListChangeListener como um lambda.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package main.java.br.com.cursojavanow; 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"); }); 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"); } } |
Saída:
Calling add(“First”):
list invalidated
strings = [First]
Calling add(0, “Zeroth”):
list invalidated
strings = [Zeroth, First]
Calling addAll(“Second”, “Third”):
list invalidated
strings = [Zeroth, First, Second, Third]
Calling set(1, “New First”):
list invalidated
strings = [Zeroth, New First, Second, Third]
Calling addAll(3, list):
list invalidated
strings = [Zeroth, New First, Second, Second_1, Second_2, Third]
Calling remove(2, 4):
list invalidated
strings = [Zeroth, New First, Second_2, Third]
Calling remove() on iterator:
list invalidated
strings = [New First, Second_2, Third]
Calling remove() on iterator:
list invalidated
strings = [Second_2, Third]
Calling removeAll(“Third”, “Fourth”):
list invalidated
strings = [Second_2]
Fonte: The Definitive Guide to Modern Java Clients with JavaFX
Deixe um comentário