我猜你提到的SortableList是FXCollections.sort中使用的那个。
ListProperty 可以实现SortableList 接口。
这确实是一个好主意,因为这将允许您选择包装列表的排序方式,例如FXCollections.sort 用于属性。在这种情况下,您可以在包含的列表中使用FXCollections.sort。
他们怎么可能?像这样:
class MyListProperty<T> extends ListPropertyBase<T> implements SortableList<T> {
...
@Override
public void sort() {
ObservableList<T> list = getValue();
if (list != null) {
FXCollections.sort((ObservableList<Comparable>) list);
}
}
@Override
public void sort(Comparator<? super T> comparator) {
ObservableList<T> list = getValue();
if (list != null) {
FXCollections.sort(list, comparator);
}
}
}
唯一的问题是,SortableList 在 com.sun.javafx.collections 包内(请参阅 It is a bad practice to use Sun's proprietary Java classes?)。
关于您与属性方案的冲突:没有,如果您按预期方式定义属性,请参阅Using JavaFX Properties and Binding section Understanding Properties
该属性将像这样实现:
private final ListProperty<MyClass> someList = ...;
public ObservableList<MyClass> getSomeList() {
return someList.get();
}
public void setSomeList(ObservableList<MyClass> newList) {
someList.set(newList);
}
public ListProperty<MyClass> someListProperty() {
return someList;
}
ListProperty 必须确保向其注册的ListChangeListeners 接收来自包装列表的更改事件。
也许您对 fxml 中使用的 readonly 列表属性感到困惑,但 ListProperty 不是只读的。
您仍然可以在 fxml 文件中使用此属性,但您需要使用 ObservableList 类型的值:
<!-- imports -->
<ContainingClass xmlns:fx="http://javafx.com/fxml/1">
<someList>
<FXCollections fx:factory="observableArrayList">
<!-- list content goes here -->
</FXCollections>
</someList>
</ContainingClass>