是否有一些将多个子列表绑定到一个主列表的板载方式?
我已将TreeItem
扩展为:
import java.util.List;
import javafx.scene.control.TreeItem;
import javafx.collections.ObservableList;
// Further imports
public class MyTreeItem extends TreeItem<MyItem> {
// ...
public ObservableList<MyHistoryItem> getOwnHistory() {
// some code generating data
}
public ObservableList<MyHistoryItem> getCombinedHistory(){
ObservableList<MLDFXHistoryItem> fxCombHistory = FXCollections.observableArrayList();
fxCombHistory.addAll(this.getOwnHistory());
for (TreeItem<MyItem> child: this.getChildren())
{
fxCombHistory.addAll(((MyItem) child).getCombinedHistory());
}
return fxCombHistory;
}
}
函数getCombinedHistory()
应将根项目中的数据与其所有子项目组合在一起。基本上它有效。无论如何,子列表中的更改不会自动转发。
我是否必须编写适当的侦听器来自行更新fxCombHistory
,或者更简单的方法来绑定此列表?
答案 0 :(得分:0)
由于我无法找到执行此操作的方法,因此我编写了我想要分享的onw课程。主界面为removeSublist(int i)
和addSublist(ObservableList<E> list)
。不允许直接add()
或remove()
数据,这会导致数据不一致。
对实施的反馈欢迎。 (可以增强对错误输入的处理。)
import java.util.ArrayList;
import java.util.List;
import javafx.collections.ModifiableObservableListBase;
import javafx.collections.ObservableList;
import javafx.collections.ListChangeListener;
public class ObservableCombindedList<E> extends ModifiableObservableListBase<E> {
private List<E> mainList = new ArrayList<E>();
private List<ObservableList<E>> subLists = new ArrayList<>();
private List<SubListListener> listeners = new ArrayList<>();
private class SubListListener implements ListChangeListener<E>{
public SubListListener (){
}
@Override
public void onChanged(ListChangeListener.Change<? extends E> c) {
ObservableList<E> subList = (ObservableList<E>) c.getList();
int subListPos = ObservableCombindedList.this.subLists.indexOf(subList);
int idxstart = getStartIndex(subListPos);
while (c.next()) {
if (c.wasPermutated() || c.wasUpdated()) {
for (int i = c.getFrom(); i < c.getTo(); ++i) {
ObservableCombindedList.this.set(i+idxstart,(E) subList.get(i));
}
}
if (c.wasRemoved()) {
ObservableCombindedList.this.remove(c.getFrom(),c.getTo());
}
if (c.wasAdded()){
for (int i = c.getFrom(); i < c.getTo(); ++i) {
ObservableCombindedList.this.add(i+idxstart,(E) subList.get(i));
}
}
};
} // void onChanged
} // private class SubListListener implements ListChangeListener<E>
public int getStartIndex(int listNr)
{
int index = 0;
for (int i_list = 0; i_list < listNr ; ++i_list)
{
index += this.subLists.get(i_list).size();
}
return index;
}
public void addSublist(ObservableList<E> list){
SubListListener listener = new SubListListener();
this.subLists.add(list);
list.addListener(listener);
this.listeners.add(listener);
for (E el : list)
{
this.add(el);
}
} // public void addSublist
public void removeSublist(int i){
int i_start = getStartIndex(i);
this.remove(i_start, i_start + this.subLists.get(i).size());
ObservableList<E> sublist = this.subLists.remove(i);
ObservableCombindedList<E>.SubListListener listener = this.listeners.remove(i);
sublist.removeListener(listener);
}
@Override
public E get(int index) {
return this.mainList.get(index);
}
@Override
public int size() {
return this.mainList.size();
}
@Override
protected void doAdd(int index, E element) {
this.mainList.add(index, element);
}
@Override
protected E doSet(int index, E element) {
return this.mainList.set(index, element);
}
@Override
protected E doRemove(int index) {
return this.mainList.remove(index);
}
}