I have an ObservableList<Person>
, where a Person
has firstName
and lastName
properties (in the appropriate JavaFX way), and I want to make a ListView
that will display the names of the people and reflect changes in both the list and the properties of the individual Person
objects in the list. How is this best done? There are two issues:
We need to make the
ListView
observe the two name properties so it can refresh changes. One way to do this is explained in this answer (see also this answer). However, this solution requires passing an "extractor" to the constructor of theObservableList
, and my list already exists (as part of a larger data model). One would think there would be a way to wrap the existingObservableList
to add an extractor, but I don't see it in the API. (Well, there is this method, but it treats the list being wrapped as simply aList
and not anObservableList
, so updates to the original list aren't reported. There is also this method that creates a "synchronized" wrapper of anObservableList
, but it doesn't include an extractor parameter.) Perhaps I should just implement a method to do this wrapping myself?We need to render the
Person
items in theListView
. I know how to do this using a customListCell
class, but I'm hoping there might be an easier way since I'm only displaying strings. Relying onPerson.toString
is not the right thing to do, since I may have other views ofPerson
that require different conversions ofPerson
toString
. Is there any way to pass aListView<Person>
aCallback<Person,String>
(or something equivalent) to convert the items to strings?
So, in the end, I do have a way to do what I want: write my own wrapper in 1 and use a custom cell factory in 2. I just feel like this requires more work on my part than it should for what I'd think is a relatively common situation. Is there an easier way I'm missing?