JavaFX ObservableList Tutorial

JavaFX ObservableList

JavaFX ObservableList is JavaFX SDK’s special implementation of List interface. It is basically an observable list that provides option to attach listeners for list content change. ObservableList has an integral role in the JavaFX development because it is used for major components like TableView, ComboBox, etc.

In this tutorial, we will explore the JavaFX ObservableList and see how we can utilize its special functionalities.

Creating and using ObservableList

You cannot instantiate an ObservableList class directly using a new call. Instead, FXCollections should be used to create an ObservableList. Let’s see how we can create an ObservableList of String type. The most common way to create it is using observableArrayList() function.

ObservableList<String> listInstance = FXCollections.observableArrayList();
//Add a single entry
listInstance.add("Java");
System.out.println(listInstance);

//Add multiple entries
listInstance.addAll("Cpp", "C#", "Python");
System.out.println(listInstance);

//Remove entry
listInstance.remove("Cpp");
System.out.println(listInstance);

As you have already noted, the basic operations are similar to normal lists, except the addAll() function availability. You can add multiple entries to the ObservableList using comma separated values. The output of the above code is as follows.

[Java]
[Java, Cpp, C#, Python]
[Java, C#, Python]

Now, let’s see some special advanced features of the ObservableList.

Listening for changes in the ObservableList

ObservableList allows us to add listeners into the list so that we can execute some logic when the contents of the list is changed. In the previous code snippet, we have added system.out.println after each change we made into the list to print the list. Instead, we can actually attach a listener to the ObservableList and it will help us to print the list as soon as it is changed. To achieve this, we will make use of javafx.collections.ListChangeListener. The onChanged() function will be called as soon as any change is made to the contents of the list.

ObservableList<String> listInstance = FXCollections.observableArrayList();

//Add a ListChangeListener that will execute as soon as the contents of the list is changed.
listInstance.addListener(new ListChangeListener<String>() {
  @Override
  public void onChanged(Change<? extends String> c) {
    System.out.println(listInstance);
  }
});

//Manipulate contents of the list
listInstance.add("Java");
listInstance.addAll("Cpp", "C#", "Python");
listInstance.remove("Cpp");

The output of the code snippet is given below. The “System.out.println(listInstance);” was executed three times, once after each list change.

[Java]
[Java, Cpp, C#, Python]
[Java, C#, Python]

ObservableList also provides a second type of listener called InvalidationListener. Unlike ListChangeListener that provides the actual change as a parameter to the onChanged function, it doesn’t provide the change. You can read more about it in the dedicated observable tutorial.

Getting values from the ObserableList is similar to getting value from normal lists in Java. To get an item at specific index, you can call “observableList.get(index)”. You can also get iterator through observableList.iterator() function.

Video Tutorial for JavaFX ObservableList

If you are interested in learning more about the ObservableList, please have a look into this ObservableList video tutorial. The video shows how to make use of the ObservableList for JavaFX ListView with realworld example.

ObservableList size change listener

Thanks to JavaFX Binding class, we can add listeners to some observable properties of the list, like the size of the list. This way, similar to how we tracked the list-content-change using a listener, we can also easily track the list size. Let’s adjust the above list content listener code to list size listener code and learn how to create a binding value to an ObservableList’s size and add a listener to it.

ObservableList<String> listInstance = FXCollections.observableArrayList();

//Listen for changes in list size
IntegerBinding listSizeProperty = Bindings.size(listInstance);
listSizeProperty.addListener(new ChangeListener<Number>() {
  @Override
  public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
    System.out.println("Updated list size: " + newValue);
  }
});

listInstance.add("something");
listInstance.addAll("something1", "random_data", "something2");
listInstance.remove("somethin3");

Output of the above code snippet is given below. As you can see, the size of the list is tracked and printed for each 3 changes we have made.

Updated list size: 1
Updated list size: 4
Updated list size: 3

JavaFX Observable Program Output

Conclusion

In this tutorial, we have learned how to create and use ObservableList in JavaFX. It provides all the functionalities of normal Java lists along with cool observable support. You might also be interested in the other articles on JavaFX like JavaFX Scene Switch Animation and JavaFX Animation Tutorial.

Muhammed Afsal Villan
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

32 COMMENTS