I have two views (JPanel) that uses the same domain object. My domain object contains a ObservableList.
The ObservableList is a LinkedList
private ObservableList<MyObject> listMyObject = ObservableCollections.
observableList(Collections.synchronizedList(new LinkedList<MyObject>()));
In my two views I to do some calculation each time an element is added to the list
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);
}
The problem I have is that as both views use the same list each time one element is added to the list the location is updated two times the object that is moving in the view finish its animation two times faster. I would like it to be updated only one time for each element added.
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);
}
}
If i don’t create view2, everything is working fine. With view2 created each time an element is added each view iteratate the list and change the location of my object two times instead of one time.
Thank you for help.
Actually, I don’t understand what you expect:
To solve, do the manipulation of the elements once outside of the view, f.i. by keeping the listener outside: