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.

Comments

48 responses to “JavaFX ObservableList Tutorial”

  1. … [Trackback]

    […] There you will find 12335 more Info on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  2. … [Trackback]

    […] There you will find 82490 more Info to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  3. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  4. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  5. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  6. … [Trackback]

    […] Here you will find 62441 more Information to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  7. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  8. … [Trackback]

    […] Find More on to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  9. … [Trackback]

    […] Here you will find 93416 additional Information to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  10. … [Trackback]

    […] Information on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  11. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  12. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  13. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  14. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  15. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  16. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  17. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  18. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  19. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  20. … [Trackback]

    […] Here you will find 62284 more Information on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  21. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  22. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  23. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  24. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  25. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  26. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  27. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  28. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  29. … [Trackback]

    […] Here you will find 55112 more Information on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  30. … [Trackback]

    […] Here you will find 76762 more Information on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  31. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  32. … [Trackback]

    […] Read More Information here on that Topic: genuinecoder.com/javafx-observable-list-tutorial/ […]

  33. order androxal online canada

    buy cheap androxal buy dublin

  34. buy enclomiphene cost without insurance

    ordering enclomiphene usa discount

  35. purchase rifaximin uk online

    buying rifaximin purchase uk

  36. how to buy xifaxan cheap canada

    online order xifaxan generic when available

  37. staxyn no rx

    cheap staxyn price in us

  38. low cost avodart from india

    avodart online prescriptions with no membership

  39. dutasteride online canada compare price

    ordering dutasteride purchase from canada

  40. flexeril cyclobenzaprine no prescription canadian

    flexeril cyclobenzaprine without prescription cheap

  41. order gabapentin generic alternative

    purchase gabapentin usa overnight delivery

  42. how to buy fildena buy online canada

    ordering fildena generic version

  43. how to buy itraconazole cheap online canada

    itraconazole no perscription usa fedex shipping

  44. levné kamagra z indie

    pナ册s pultovテス prodej kamagra v irsku

  45. générique kamagra canadien sans ordonnance

    generique kamagra gratuit buy online

Leave a Reply