I am trying to set items to a tableview but the setitems method expects an observablelist while I have an observableset in my model.The FXCollections utility class does not have a method for creating an observable list given an observable set.I tried casting but that caused a class cast exception (as expected).
Currently I am using this kind of code
new ObservableListWrapper<E>(new ArrayList<E>(pojo.getObservableSet()));
And I have some problems with it:
- Will editing this in the table update the underlying set as expected?
- Is it the 'right' way of doing this
So in short I need a style guide or best practice for converting between observable set and observable list because I expect to be doing this a lot when building a java fx GUI
Will editing this in the table update the underlying set as expected ?
No because, you are doing a copy of the set:
Is it the 'right' way of doing this ?
I think the right way is not doing it.
Set
are notList
and vice versa. Both have specific contraints. For example, the lists are ordered and sets contains no duplicate elements.Moreover, nor
FXCollections
norBindings
provides this kind of stuff.I would like the collection to remain as a set to enforce uniqueness
I guess you could write a custom
ObservableList
, for example theParent::children
have a similar behavior. It throws anIllegalArgumentException
if a duplicate children is added. If you look at the source code, you will see that it is aVetoableListDecorator
extension. You could write your own:Just in Case someone stumbles over this question looking for a one-way to convert an ObservableSet into an ObservableList... I post my solution. It doesn't support feeding back data to the set (which in my opinion wouldn't be nice since TableView doesn't have a concept of not being able to change a value) but supports updates of the set and preserves the (in this case) sorted order.