如何将 JavaFX ListView 与复杂数据一起使用?
How to use JavaFX ListView with complex data?
我有一个 ObservableList<Person>
,其中 Person
有 firstName
和 lastName
属性(以适当的 JavaFX 方式),我想制作一个 ListView
将显示人员姓名并反映列表和列表中各个 Person
对象的属性的更改。如何做到最好?有两个问题:
我们需要让 ListView
观察两个名称属性,以便它可以刷新更改。 this answer (see also this answer). However, this solution requires passing an "extractor" to the constructor of the ObservableList
, and my list already exists (as part of a larger data model). One would think there would be a way to wrap the existing ObservableList
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 a List
and not an ObservableList
, so updates to the original list aren't reported. There is also this method 中解释了一种方法,它创建了 ObservableList
的 "synchronized" 包装器,但它不包含提取器参数。)也许我应该只实现一个方法来做把自己裹起来了?
我们需要渲染 ListView
中的 Person
项。我知道如何使用自定义 ListCell
class 执行此操作,但我希望可能有更简单的方法,因为我只显示字符串。依赖 Person.toString
不是正确的做法,因为我可能对 Person
有其他看法,需要从 Person
到 String
的不同转换。有没有办法通过 ListView<Person>
和 Callback<Person,String>
(或等效的东西)将项目转换为字符串?
所以,最后,我确实有办法做我想做的事:在 1 中编写我自己的包装器并在 2 中使用自定义单元工厂。我只是觉得这需要我做更多的工作应该是我认为相对常见的情况。有没有我想念的更简单的方法?
这已在 Add extractor to existing ObservableList 的评论中得到回答。
使用提取器创建一个新的可观察列表,并将内容双向绑定到现有列表。类似于
ListView<Person> listView = new ListView<>();
ObservableList<Person> personList = FXCollections.observableList(person ->
new Observable[] {person.firstNameProperty(), person.lastNameProperty()});
Bindings.bindContentBidirectional(model.getPersonList(), personList);
listView.setItems(personList);
我有一个 ObservableList<Person>
,其中 Person
有 firstName
和 lastName
属性(以适当的 JavaFX 方式),我想制作一个 ListView
将显示人员姓名并反映列表和列表中各个 Person
对象的属性的更改。如何做到最好?有两个问题:
我们需要让
ListView
观察两个名称属性,以便它可以刷新更改。 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 中解释了一种方法,它创建了ObservableList
的 "synchronized" 包装器,但它不包含提取器参数。)也许我应该只实现一个方法来做把自己裹起来了?我们需要渲染
ListView
中的Person
项。我知道如何使用自定义ListCell
class 执行此操作,但我希望可能有更简单的方法,因为我只显示字符串。依赖Person.toString
不是正确的做法,因为我可能对Person
有其他看法,需要从Person
到String
的不同转换。有没有办法通过ListView<Person>
和Callback<Person,String>
(或等效的东西)将项目转换为字符串?
所以,最后,我确实有办法做我想做的事:在 1 中编写我自己的包装器并在 2 中使用自定义单元工厂。我只是觉得这需要我做更多的工作应该是我认为相对常见的情况。有没有我想念的更简单的方法?
这已在 Add extractor to existing ObservableList 的评论中得到回答。
使用提取器创建一个新的可观察列表,并将内容双向绑定到现有列表。类似于
ListView<Person> listView = new ListView<>();
ObservableList<Person> personList = FXCollections.observableList(person ->
new Observable[] {person.firstNameProperty(), person.lastNameProperty()});
Bindings.bindContentBidirectional(model.getPersonList(), personList);
listView.setItems(personList);