Implement ObservableList, extend ObservableListWrapper

I want to create a class that is almost identical to the object returned by FXCollections.observableArrayList() , but with some additional functions. My first thought was something like

 public class MyObservableList implements ObservableList { private ObservableList list = FXCollections.observableArrayList(); public functionWhatever() { // whatever } } 

but that means overriding the 30 functions that come with the ObservableList (which seems like a hint that I'm doing something wrong).

FXCollections.observableArrayList() returns an object of type com.sun.javafx.collections.ObservableListWrapper , but when I extend ObservableListWrapper , I need to create a constructor of type

 MyObservableList( List arg0 ) 

or

 MyObservableList( List arg0, Callback arg1 ) 

which bothers me because FXCollections.observableArrayList() does not accept any arguments.

I don’t know how FXCollections creates an ObservableListWrapper object that it returns, but I want MyObservableList be identical to the object returned by FXCollections (plus a few extra features).

How to do it?

+6
source share
1 answer

Extend SimpleListProperty docs.oracle.com

This class provides a complete implementation of a property that wraps an ObservableList.


Pay attention to this ctor:

public SimpleListProperty (InitialValue ObservableList)

So you can:

 public class MyObservableList extends SimpleListProperty { //constructor MyObservableList(){ super(FXCollections.observableArrayList()); } public functionWhatever() { // whatever } } 

This way your class will be based on ArrayList.

+2
source

All Articles