Collections in JavaFX are defined by the javafx.collections package, which consists of the following interfaces and classes:
interface | Description |
---|---|
ObservableList | A list that allows us to track changes |
ListChangeListener | An interface that receives notifications of changes |
ObservableMap | A map that allows us to track changes |
MapChangeListener | An interface that receives notifications of changes from an ObservableMap |
Class | Description |
---|---|
FXCollections | A utility class maps to java.util.Collections |
ListChangeListener.Change | Represents a change made to an ObservableList |
MapChangeListener.Change | Represents a change made to an ObservableMap |
The following code shows how to use ObservableList, ObservableMap, and FXCollections.
import java.util.ArrayList; import java.util.List; //from w ww .j a v a 2 s . c o m import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<String>(); ObservableList<String> observableList = FXCollections.observableList(list); observableList.addListener(new ListChangeListener() { @Override public void onChanged(ListChangeListener.Change change) { System.out.println("change!"); } }); observableList.add("item one"); list.add("item two"); System.out.println("Size: " + observableList.size()); } }
The code above generates the following result.
The following code shows how to listen for changes on an ObservableMap.
import java.util.HashMap; import java.util.Map; /*from www . j a v a 2 s . co m*/ import javafx.collections.FXCollections; import javafx.collections.MapChangeListener; import javafx.collections.ObservableMap; public class Main { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); ObservableMap<String, String> observableMap = FXCollections .observableMap(map); observableMap.addListener(new MapChangeListener() { @Override public void onChanged(MapChangeListener.Change change) { System.out.println("change! "); } }); observableMap.put("key 1", "value 1"); map.put("key 2", "value 2"); } }
The code above generates the following result.
The following code shows how to find out what has been changed.
import java.util.ArrayList; import java.util.List; //w w w .ja va2 s. c o m import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<String>(); ObservableList<String> observableList = FXCollections.observableList(list); observableList.addListener(new ListChangeListener() { @Override public void onChanged(ListChangeListener.Change change) { System.out.println("Detected a change! "); while (change.next()) { System.out.println("Was added? " + change.wasAdded()); System.out.println("Was removed? " + change.wasRemoved()); System.out.println("Was replaced? " + change.wasReplaced()); System.out.println("Was permutated? " + change.wasPermutated()); } } }); observableList.add("item one"); list.add("item two"); System.out.println("Size: " + observableList.size()); } }
The code above generates the following result.