Updated April 5, 2023
Introduction to JavaFX ObservableList
In JavaFX, collections are defined by the package known as javafx.collections that contains different interfaces such as ObservableList, ListChangeListener , ObservableMap , MapChangeListener, and classes such as FXCollections, ListChangeListener.Change, MapChangeListener.Change etc. In this article, we will be focusing on the list known as ObservableList that permits tracking the changes when occurred. ListChangeListener.Change is a class that denotes the change that has been made to an ObservableList. First, we will see how to declare an ObservableList in JavaFX.
Declaration
Below is the declaration of ObservableList in JavaFX.
public interface ObservableList<E> extends List<E>, Observable
Methods
Following are the different methods of ObservableList in JavaFX.
1. addListener
Syntax:
void addListener(ListChangeListener<? super E> li)
Description: A listener li will be added to the observable list.
Parameter: li, the listener that listens to the changes in the list.
2. removeListener
Syntax:
void removeListener(ListChangeListener<? super E> li)
Description: A listener li will be removed from the observable list. If it is not available, nothing will happen.
Parameter: li, the listener that has to be removed.
3. addAll
Syntax:
boolean addAll(E... el)
Description: A suitable method for the var-arg addition of elements.
Parameter: el, elements that have to be added.
Return Value: true, which is mentioned by Collection.add(E)
4. setAll
Syntax:
boolean setAll (E... el)
Description: A suitable method for var-arg addition of elements after clearing the ObservableList.
Parameter: el, elements that have to be set.
Return Value: true, which is mentioned by Collection.add(E)
5. setAll
Syntax:
boolean setAll (Collection<? extends E> c)
Description: A suitable method for adding all the elements from the collection after clearing the ObservableList.
Parameter: c, the collection that contains elements that have to be added to the list.
Return Value: true, which is mentioned by Collection.add(E)
6. removeAll
Syntax:
boolean removeAll (E... el)
Description: A suitable method for var-arg removal of elements.
Parameter: el, elements that have to be removed.
Return Value: true if the ObservableList is changed due to calling this method.
7. retainAll
Syntax:
boolean retainAll (E... el)
Description: A suitable method for var-arg retaining of elements.
Parameter: el, elements that have to be retained.
Return Value: true if the ObservableList is changed due to calling this method.
8. remove
Syntax:
void remove(int from, int to)
Description: Similar to sublist(from, to).clear(). ObservableList used this method for the same.
Parameter: from, which is the starting of the range that has to be removed, and to, which is the ending of the range that has to be removed,
9. filtered
Syntax:
default FilteredList<E> filtered(Predicate<E> pr)
Description: A FilteredList wrapper is created for this list by using the mentioned predicate.
Parameter: pr, a predicate that has to be used.
Return Value: A new filtered list.
10. sorted
Syntax:
default SortedList<E> sorted( Comparator<E> c)
Description: A SortedList wrapper is created for this list by using the mentioned comparator.
Parameter: c, a comparator that has to be used. If natural order, then null.
Return Value: A new sorted list.
11. sorted
Syntax:
default SortedList<E> sorted( )
Description: A SortedList wrapper is created for this list by natural ordering.
Return Value: A new sorted list.
Examples of JavaFX ObservableList
Now, we will see a few examples of ObservableList in JavaFX.
Example #1
JavaFX program to demonstrate ObservableList that consists of integer type elements.
Code:
//sample program to demonstrate the working of ObservableList
import java.util.List;
import java.util.ArrayList;
import javafx.collections.ObservableList;
import javafx.collections.ListChangeListener;
import javafx.collections.FXCollections;
//main class
public class ObservableListSample {
//main method
public static void main(String[] args)
{
//create a list of integer type
List<Integer> li = new ArrayList<Integer>();
//create an observable list
ObservableList<Integer> oli = FXCollections.observableList(li);
//add listener method
oli.addListener(new ListChangeListener() {
@Override
//onChanged method
public void onChanged(ListChangeListener.Change c) {
System.out.println("Hey, a change occured. . . ");
}
});
//add an item to the observable List
oli.add(22);
System.out.println("Size of the observable list is: " + oli.size() );
li.add(44);
System.out.println("Size of the observable list is: " + oli.size());
oli.add(66);
System.out.println("Size of the observable list is: " + oli.size());
}
}
Output:
In this program, the first ObservableList is imported along with other packages. Then, create a list of integer types followed by the creation of an observable list. After this, add the listener method and onChanged method. Once this is done, add items to the observable list. On executing the code, items are added to the list, and the size of the list gets displayed.
Example #2
JavaFX program to demonstrate ObservableList that consists of string type elements.
Code:
//sample program to demonstrate the working of ObservableList
import java.util.List;
import java.util.ArrayList;
import javafx.collections.ObservableList;
import javafx.collections.ListChangeListener;
import javafx.collections.FXCollections;
//main class
public class ObservableListSample {
//main method
public static void main(String[] args)
{
//create a list of string type
List<String> li = new ArrayList<String>();//create an observable list
ObservableList<String> oli = FXCollections.observableList(li);
//add listener method
oli.addListener(new ListChangeListener() {
@Override
//onChanged method
public void onChanged(ListChangeListener.Change c) {
System.out.println("Hey, a change occured. . . ");
//check
while (c.next()) {
//if anything was added
System.out.println("Hey, anything added ? " + c.wasAdded());
System.out.println("Hey, anything removed ? " + c.wasRemoved());
}
}
});
//add an item to the observable List
oli.add("This is item 1");
System.out.println("Size of the observable list is: " + oli.size() + oli.toString());
li.add("This is item 2" );
System.out.println("Size of the observable list is: " + oli.size() + oli.toString());
oli.add("This is item 3");
System.out.println("Size of the observable list is: " + oli.size() + oli.toString());
li.add("This is item 4");
System.out.println("Size of the observable list is: " + oli.size() + oli.toString());
//remove
oli.remove(1);
System.out.println("Size of the observable list is: " + oli.size()+oli.toString());
oli.sort(null);
System.out.println("Size of the observable list is: " + oli.size()+oli.toString());
oli.set(2, "This is item 5");
System.out.println("Size of the observable list is: " + oli.size()+oli.toString());
}
}
Output:
In this program also, the first ObservableList is imported along with other packages. Then, create a list of string types followed by the creation of an observable list. After this, different methods are used. On executing the code, items are added to the list, and the size and the items of the list are displayed.
Conclusion
ObservableList is a list in JavaFX that permits tracking the changes when occurred. In this article, different aspects such as declaration, methods, and examples of ObservableList is explained in detail.
Recommended Articles
This is a guide to JavaFX ObservableList. Here we discuss the Examples of JavaFX ObservableList along with the codes and outputs. You may also have a look at the following articles to learn more –