A classe FXCollections contém métodos de fábrica para criação de observable collections e arrays. Eles parecem com os métodos da fábrica do java.util.Collections exceto aquele que retorna observable collections e arrays.
Eles são os únicos meios através da qual a instância fornecida pelo sistema é criada: observable collections e arrays.
A classe FXCollections fornece poucos métodos para manipular o objetos do ObservableList criados. Isso incluí o copy(), fill(), replaceAll(), reverse(), rotate(), shuffle() e sort().
Eles realizam a mesma funcionalidade da java.util.Collections, exceto que eles minimizam as notificações geradas pelas mudanças.
Exemplo utilizando os métodos do FxCollections.
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
package main.java.br.com.cursojavanow; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.collections.MapChangeListener; import javafx.collections.ObservableFloatArray; import javafx.collections.ObservableList; import javafx.collections.ObservableMap; import javafx.collections.ObservableSet; import javafx.collections.SetChangeListener; import java.util.Arrays; import java.util.Comparator; import java.util.Random; public class FXCollectionsExample { public static void main(String[] args) { ObservableList<String> list = FXCollections.observableArrayList(); ObservableMap<String, String> map = FXCollections.observableHashMap(); ObservableSet<Integer> set = FXCollections.observableSet(); ObservableFloatArray array = FXCollections.observableFloatArray(); list.addListener((ListChangeListener<String>) c -> { System.out.println("\tlist = " + c.getList()); }); map.addListener((MapChangeListener<String, String>) c -> { System.out.println("\tmap = " + c.getMap()); }); set.addListener((SetChangeListener<Integer>) c -> { System.out.println("\tset = " + c.getSet()); }); array.addListener((observableArray, sizeChanged, from, to) -> { System.out.println("\tarray = " + observableArray); }); manipulateList(list); manipulateMap(map); manipulateSet(set); manipulateArray(array); } private static void manipulateList( ObservableList<String> list) { System.out.println("Calling list.addAll(\"Zero\"," + " \"One\", \"Two\", \"Three\"):"); list.addAll("Zero", "One", "Two", "Three"); System.out.println("Calling copy(list," + " Arrays.asList(\"Four\", \"Five\")):"); FXCollections.copy(list, Arrays.asList("Four", "Five")); System.out.println("Calling replaceAll(list," + " \"Two\", \"Two_1\"):"); FXCollections.replaceAll(list, "Two", "Two_1"); System.out.println("Calling reverse(list):"); FXCollections.reverse(list); System.out.println("Calling rotate(list, 2):"); FXCollections.rotate(list, 2); System.out.println("Calling shuffle(list):"); FXCollections.shuffle(list); System.out.println("Calling shuffle(list," + " new Random(0L)):"); FXCollections.shuffle(list, new Random(0L)); System.out.println("Calling sort(list):"); FXCollections.sort(list); System.out.println("Calling sort(list, c)" + " with custom comparator: "); FXCollections.sort(list, new Comparator<String>() { @Override public int compare(String lhs, String rhs) { // Reverse the order return rhs.compareTo(lhs); } }); System.out.println("Calling fill(list," + " \"Ten\"): "); FXCollections.fill(list, "Ten"); } private static void manipulateMap( ObservableMap<String, String> map) { System.out.println("Calling map.put(\"Key\"," + " \"Value\"):"); map.put("Key", "Value"); } private static void manipulateSet( ObservableSet<Integer> set) { System.out.println("Calling set.add(1024):"); set.add(1024); } private static void manipulateArray( ObservableFloatArray array) { System.out.println("Calling array.addAll(3.14159f," + " 2.71828f):"); array.addAll(3.14159f, 2.71828f); } } |
Criamos um observable list, um observable map, um observable set e um observable array utilizando a fábrica de métodos FXCollections, adicionado listeners neles, e manipulando eles de alguma maneira, incluindo utilizar métodos úteis do FXCollections para o list e métodos ObservableFloatArray do array.
A saída desse programa será:
Calling list.addAll(“Zero”, “One”, “Two”, “Three”):
list = [Zero, One, Two, Three]
Calling copy(list, Arrays.asList(“Four”, “Five”)):
list = [Four, Five, Two, Three]
Calling replaceAll(list, “Two”, “Two_1”):
list = [Four, Five, Two_1, Three]
Calling reverse(list):
list = [Three, Two_1, Five, Four]
Calling rotate(list, 2):
list = [Five, Four, Three, Two_1]
Calling shuffle(list):
list = [Five, Two_1, Three, Four]
Calling shuffle(list, new Random(0L)):
list = [Four, Five, Two_1, Three]
Calling sort(list):
list = [Five, Four, Three, Two_1]
Calling sort(list, c) with custom comparator:
list = [Two_1, Three, Four, Five]
Calling fill(list, “Ten”):
list = [Ten, Ten, Ten, Ten]
Calling map.put(“Key”, “Value”):
map = {Key=Value}
Calling set.add(1024):
set = [1024]
Calling array.addAll(3.14159f, 2.71828f):
array = [3.14159, 2.71828]
Fonte: The Definitive Guide to Modern Java Clients with JavaFX
Deixe um comentário