我有兩個使用相同域對象的視圖(JPanel)。我的域對象包含一個ObservableList
。同一觀察列表在幾個視圖
的ObservableList是一個LinkedList
private ObservableList<MyObject> listMyObject = ObservableCollections.
observableList(Collections.synchronizedList(new LinkedList<MyObject>()));
在我的兩個觀點我每一個元素被添加到列表的時間做一些計算
protected class MyListDataListener implements ObservableListListener {
public void listElementsAdded(ObservableList list, int index, int length) {
MyObject obj = (MyObject)list.get(index);
Poin2D location = obj.getObjLocation();
location.setLocation(location.x + (time/getWidth()), location.y);
obj.setObjLocation(location);
}
我的問題是,由於雙方意見每次將一個元素添加到列表中時使用相同的列表位置更新兩次在視圖中移動的對象快兩倍地完成其動畫。我希望每增加一個元素都只更新一次。
public class MyFrame extends JFrame {
public MyFrame() {
View view1 = new View(domainObject.getMyDataList());
View view2 = new View(domainObject.getMyDataList());
}
}
public class View extends JPanel {
private ObservableList<MyObject> listMyObject;
private ObservableList<MyObject> otherList = ObservableCollections.
observableList(Collections.synchronizedList(new LinkedList<MyObject>()));
public View(ObservableList<MyObject> listMyObject) {
this.listMyObject = listMyObject;
listMyObject.addListListener(new MyListDataListener());
}
protected class MyListDataListener implements ObservableListListener {
public void listElementsAdded(ObservableList list, int index, int length) {
otherList.add((MyObject)list.get(index));
for(MyObject obj : otherList) {
Poin2D location = obj.getObjLocation();
location.setLocation(location.x + (time/getWidth()), location.y);
obj.setObjLocation(location);
}
}
如果我不創建view2,一切工作正常。在每次添加元素時都會創建view2,每個視圖迭代列表並更改對象的位置兩次而不是一次。 謝謝你的幫助。
你能發佈更多的代碼來展示如何使用監聽器嗎? –
哪個'ObservableList'? – trashgod
不明白這個問題:如果列表觸發兩次,它的實現有些問題。如果列表沒有一個getSize()或類似的方法,並且它的實現也是錯誤的。如果在同一視圖(?)中對同一對象執行重定位時有兩個單獨的偵聽器,則使用代碼 – kleopatra