2

For example, if you have an ArrayList, you can do the following:

ArrayList<T> list = new ArrayList<T>();
ObservableList<T> data = FXCollections.observableArrayList(list);

Similarly, if I have a custom-made Stack instead of an ArrayList, how would I make an ObservableList off of that?

Jeff
  • 67
  • 1
  • 9
  • Are we talking `java.util.Stack` or a home-grown implementation? If it is home-grown, could you post its structure? – Tunaki Jan 30 '16 at 21:28
  • The [Javadocs for `ModifiableObservableListBase`](http://docs.oracle.com/javase/8/javafx/api/javafx/collections/ModifiableObservableListBase.html) show how to wrap an arbitrary structure to make it observable, for the case if your `Stack` is not a `Collection` implementation. – James_D Jan 30 '16 at 22:04
  • 1
    Also note that `FXCollections.observableArrayList(collection)` creates a new observable list, and copies the elements of the provided collection to it. The observable list in this case (unlike `FXCollections.observableList(list)`) is not backed by the provided list: changes to the observable list do not propagate to the provided list, and vice-versa. So all you need to replicate this behavior is a mechanism to get all the elements of your stack, as either a collection or an array. – James_D Jan 31 '16 at 02:03

1 Answers1

1

One way would be to implement the List interface in your custom stack. As you can see in the documentation there is an FX.Collections#observableList(List<E>) method provided.

So...

public class MyStack<T> implements List<T> {
    // Provide the list interface...
}

and pass it directly.

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75