JavaFX Collection's ObservableList and ObservableMap
Collections in JavaFX are defined by the javafx.collections package, which consists of the following interfaces and classes:
Interfaces:
ObservableList: A list that enables listeners to track changes when they occur
ListChangeListener: An interface that receives notifications of changes to an ObservableList
ObservableMap: A map that enables observers to track changes when they occur
MapChangeListener: An interface that receives notifications of changes to an ObservableMap
Classes:
FXCollections: A utility class that consists of static methods that are one-to-one copies of java.util.Collections methods
ListChangeListener.Change: Represents a change made to an ObservableList
MapChangeListener.Change: Represents a change made to an ObservableMap
Example of ObservableList
Here a standard List is first created. It is then wrapped with an ObservableList. A ListChangeListener is then registered, and will receive notification whenever a change is made on the ObservableList :
packageorg.attune.collection;
importjava.util.List;
importjava.util.ArrayList;
importjavafx.collections.ObservableList;
importjavafx.collections.ListChangeListener;
importjavafx.collections.FXCollections;
public class ObservableListDemo {
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());
}
}
});
observableList.add("a : item one");
System.out.println("Size: " + observableList.size()+observableList.toString());
list.add("d : item two");
System.out.println("Size: " + observableList.size()+observableList.toString());
observableList.add("f : item Three");
System.out.println("Size: " + observableList.size()+observableList.toString());
list.add("b : item four");
System.out.println("Size: " + observableList.size()+observableList.toString());
observableList.remove(1);
System.out.println("Size: " + observableList.size()+observableList.toString());
observableList.sort(null);
System.out.println("Size: " + observableList.size()+observableList.toString());
observableList.set(2, "c : item five");
System.out.println("Size: " + observableList.size()+observableList.toString());
}
}
Here is output of above program:
Detected a change!
Was added? true
Was removed? false
Size: 1[a : item one]
Size: 2[a : item one, d : item two]
Detected a change!
Was added? true
Was removed? false
Size: 3[a : item one, d : item two, f : item Three]
Size: 4[a : item one, d : item two, f : item Three, b : item four]
Detected a change!
Was added? false
Was removed? true
Size: 3[a : item one, f : item Three, b : item four]
Detected a change!
Was added? false
Was removed? false
Size: 3[a : item one, b : item four, f : item Three]
Detected a change!
Was added? true
Was removed? true
Size: 3[a : item one, b : item four, c : item five]
Example of ObservableMap
package org.attune.collection;
import java.util.Map;
import java.util.HashMap;
import javafx.collections.ObservableMap;
import javafx.collections.MapChangeListener;
import javafx.collections.FXCollections;
public class ObservableMapDemo {
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("Detected a change! ");
}
});
// Changes to the observableMap WILL be reported.
observableMap.put("key 1","value 1");
System.out.println("Size: "+observableMap.size() + observableMap.toString());
// Changes to the underlying map will NOT be reported.
map.put("key 2","value 2");
System.out.println("Size: "+observableMap.size()+ observableMap.toString());
// Changes to the observableMap WILL be reported.
observableMap.remove("key 1");
System.out.println("Size: "+observableMap.size() + observableMap.toString());
}
}
Here is the output:
Detected a change!
Size: 1{key 1=value 1}
Size: 2{key 2=value 2, key 1=value 1}
Detected a change!
Size: 1{key 2=value 2}
Stay tuned for More Educational Content Attune World Wide
Opinions expressed by DZone contributors are their own.